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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With