Filipa Almeida
Filipa Almeida

Reputation: 29

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?

Upvotes: 0

Views: 2821

Answers (3)

Jared Goguen
Jared Goguen

Reputation: 9010

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)

Upvotes: 1

Batman
Batman

Reputation: 8917

The best way is probably to ask python to list all the files in the directory, then work on them

import os
import caffe

directory = r"/Users/Photos/Foo"
file_names = os.listdir(directory)
for file_name in file_names:
    if file_name [:14] == "ILSVRC2012_val_":
        full_path = os.path.join(directory, file_name)
        im = caffe.io.load_image(full_path)

Upvotes: 0

Gal Dreiman
Gal Dreiman

Reputation: 4009

I suggest to use absolute path like this.

the following example relevant to linux, but you can apply on windows as well.

/home/<your_directory>/ILSVRC2012_val_00000010.JPEG

Upvotes: 0

Related Questions