Skip to content Skip to sidebar Skip to footer

Python Globbing A Directory Of Images

I am working on a project currently and so far I have generated a folder of images (png format) where I need to iterate through each image and do some operation on it using PIL. I

Solution 1:

Solution with os package:

import os

source_path = "my_path"

image_files = [os.path.join(base_path, f) for f in files for base_path, _, files inos.walk(source_path) if f.endswith(".png")]

for filepath in image_files:
    callimage = Image.open(filepath).load()
    # ...

Solution using glob:

import glob

source_path = "my_path"

image_files = [source_path + '/' + f for f in glob.glob('*.png')]

for filepath in image_files:
    callimage = Image.open(filepath).load()
    # ...

Post a Comment for "Python Globbing A Directory Of Images"