Nanda
Nanda

Reputation: 403

How to select and load specific set of images in a folder using Python?

I have a folder comprising thousands of image files (all .jpg). I want to select only a specific subset of these images and load them into my program for face recognition application. I have the following code snippet for this process:

from PIL import Image
import os, os.path

images = []
path = "/path/to/images"
wanted_images = ["What goes in here?"]
for i in os.listdir(path):
    ext = os.path.splitext(i)[1]
    if ext.lower() not in wanted_images:
        continue
    images.append(Image.open(os.path.join(path,i)))

Is there a smart way to manage "What goes in here?" section of the code? Essentially, the images are labeled "1_1.jpg",...,"1_20.jpg", "2_1.jpg",...,"2_20.jpg", "3_1.jpg",...,"3_20.jpg",...,"100_1.jpg",...,"100_20.jpg". I want to select only images labeled "1_11.jpg", "2_11.jpg", "3_11.jpg", ...,"100_11.jpg".

Upvotes: 0

Views: 4574

Answers (2)

Adon Bilivit
Adon Bilivit

Reputation: 26976

In order to open all images with a basename ending with '_11.jpg' you could do this:

from PIL import Image
from glob import glob
from os import path

directory = '<your directory>'

images = [Image.open(jpg) for jpg in glob(path.join(directory, '*_11.jpg'))]

Note that the pattern matching is case sensitive so it won't identify files ending, for example, in .JPG

Upvotes: 2

Jan Jaap Meijerink
Jan Jaap Meijerink

Reputation: 427

Something like this might work. Glob can help you select paths based on patterns. The builtin pathlib library supports glob patterns nicely.

from PIL import Image
from pathlib import Path

images = []
path = "/path/to/images"
for p in Path(path).glob("**/*_11.jpg"):
    images.append(Image.open(str(p)))

Upvotes: 1

Related Questions