python read all images in a folder code example

Example 1: python get all images in directory

import glob

for filename in glob.iglob(root_dir + '**/*.jpg', recursive=True):
    # Do something

Example 2: get all type of image in folder python

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]

Example 3: python read all jpg files in a directory and display the first picture

# -*- coding: UTF-8 -*-import numpy as npimport osfrom scipy.misc import imread, imresizeimport matplotlib.pyplot as pltfrom glob import glob  # Read all the jpg image directorydef load_image(image_path, image_size):    file_name=glob(image_path+"/*jpg")    sample = []    for file in file_name:        pic = imread(file).astype(np.float32)        pic = imresize(pic, (image_size, image_size)).astype(np.float32)        sample.append(pic)     sample = np.array(sample)    return sample if __name__=='__main__':    samples=load_image("./images",150)         # Display the first picture    pic=samples[:1,:,:,:]    pic=np.reshape(pic,(150,150,3)).astype(np.uint8)    plt.imshow(pic)    plt.show()

Example 4: import all images from folder python

from PIL import Image
import glob
image_list = []
for filename in glob.glob('yourpath/*.gif'): #assuming gif
    im=Image.open(filename)
    image_list.append(im)