Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading all images using imread from a given folder

Tags:

python

opencv

Loading and saving images in OpenCV is quite limited, so... what is the preferred ways to load all images from a given folder? Should I search for files in that folder with .png or .jpg extensions, store the names and use imread with every file? Or is there a better way?

like image 450
user107986 Avatar asked May 14 '15 06:05

user107986


People also ask

How does Jupyter notebook read pictures from a folder?

To read the image using OpenCV I have defined load_images_from_folder function which takes a path where images are stored as an input parameter , In the next step cv2. imread function read all files in a folder and append them to images =[] list then return images list.


1 Answers

Why not just try loading all the files in the folder? If OpenCV can't open it, oh well. Move on to the next. cv2.imread() returns None if the image can't be opened. Kind of weird that it doesn't raise an exception.

import cv2 import os  def load_images_from_folder(folder):     images = []     for filename in os.listdir(folder):         img = cv2.imread(os.path.join(folder,filename))         if img is not None:             images.append(img)     return images 
like image 102
derricw Avatar answered Sep 28 '22 11:09

derricw