How to read all the images in the same folder?

All of my images are put in the same folder, and I want to load them.

data
    img
        0.png
        1.png
        2.png
        3.png
        ...

In case you want them to be a part of dataloader, then you can use ImageFolder.

But if you want to load them to memory, the most efficient way would be to

import os
import matplotlib.pyplot as plt
path = 'data/img/'
img_names = os.listdir(path)

images = np.empty(num_images, width, height, channels)
for idx, name in enumerate(img_names):
    img_name = path + name
    # Use you favourite library to load the image
    image = plt.imread(img_name)
    images[idx] = image
2 Likes