Loading all images using imread from a given folder

后端 未结 8 2010
长发绾君心
长发绾君心 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:18

    To add onto the answer from Rishabh and make it able to handle files that are not images that are found in the folder.

    import matplotlib.image as mpimg
    
    images = []
    folder = './your/folder/'
    for filename in os.listdir(folder):
        try:
            img = mpimg.imread(os.path.join(folder, filename))
            if img is not None:
                images.append(img)
        except:
            print('Cant import ' + filename)
    images = np.asarray(images)
    

提交回复
热议问题