Filipa Almeida Filipa Almeida - 20 days ago 5
Python Question

How to load all images in folder by its name using python?

I have a folder with 50000 images named as

ILSVRC2012_val_00000001.JPEG
until
ILSVRC2012_val_00050000.JPEG
.
I want to load each image and then used them to something. Here, that's the code I'm using (load just first 14 images):

for m in range(0,15):

count = m + 1

im = caffe.io.load_image(IMAGE_PATH_FOLDER + 'ILSVRC2012_val_000' + str(count).zfill(5) + '.JPEG')


The error is

No such file or directory: ILSVRC2012_val_00000010.JPEG


Any idea how to solve it?

Answer

I'd use glob.glob.

from glob import glob

for path in sorted(glob(IMAGE_PATH_FOLDER + "ILSVRC2012_val_*.JPEG")):
    im = caffe.io.load_image(path)