python – Loading all images using imread from a given folder

python – Loading all images using imread from a given folder

Why not just try loading all the files in the folder? If OpenCV cant open it, oh well. Move on to the next. cv2.imread() returns None if the image cant be opened. Kind of weird that it doesnt 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

I used skimage. You can create a collection and access elements the standard way, i.e. col[index]. This will give you the RGB values.

from skimage.io import imread_collection

#your path 
col_dir = cats/*.jpg

#creating a collection with the available images
col = imread_collection(col_dir)

python – Loading all images using imread from a given folder

import glob
cv_img = []
for img in glob.glob(Path/to/dir/*.jpg):
    n= cv2.imread(img)
    cv_img.append(n)`

Leave a Reply

Your email address will not be published.