1

Problem: It looks like the glob/PIL doesn't read images in subfolders. Is there a way to apply this to multiple directories/subfolders?

Purpose: List all the images in a directory, which has multiple folders (subfolders), by their name and size (resolution). I've used PIL/glob and so far, this has worked well on a single folder. However, when I try to apply the same logic on a folder that has over 1000 subfolders, the list returns no value.

Here is the code I've written using PIL, glob, and os:

from PIL import Image
import glob, os

im=[]

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    print(file, im.size)

The above code lists all the images in this format - e.g., NYC (3795, 1588).

I'd appreciate your suggestions. Thank you!

AMC
  • 2,642
  • 7
  • 13
  • 35
Krish
  • 11
  • 2

1 Answers1

1

With a directory tree like this:

.
└── dirA
    ├── dirB
    │   ├── fileA
    │   └── fileB
    ├── fileA
    └── fileB

Using recursive glob you are able to get the result you want:

>>> from glob import glob
>>> glob('./**/*', recursive=True)
['./dirA', './dirA/fileA', './dirA/fileB', './dirA/dirB', './dirA/dirB/fileA', './dirA/dirB/fileB']

**: recursively match any number of layers of non-hidden directories

*: matches any number of any characters including none

source

Community
  • 1
  • 1
Gustavo
  • 668
  • 13
  • 24