Loading all images using imread from a given folder

后端 未结 8 1990
长发绾君心
长发绾君心 2021-01-30 21:48

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

8条回答
  •  鱼传尺愫
    2021-01-30 22:13

    If all images are of the same format:

    import cv2
    import glob
    
    images = [cv2.imread(file) for file in glob.glob('path/to/files/*.jpg')]
    

    For reading images of different formats:

    import cv2
    import glob
    
    imdir = 'path/to/files/'
    ext = ['png', 'jpg', 'gif']    # Add image formats here
    
    files = []
    [files.extend(glob.glob(imdir + '*.' + e)) for e in ext]
    
    images = [cv2.imread(file) for file in files]
    

提交回复
热议问题