简体   繁体   中英

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?

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)

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM