list_images now stores valid image and PIL exts in sets built ahead of time rather than on each iteration. exact_path is not created unless the file's ext is a valid image ext.

This commit is contained in:
Samuel J. Woodward 2025-01-06 09:06:37 -05:00
parent fea8b11cf5
commit ca9ecbb3ca

View File

@ -23,18 +23,15 @@ def list_images(path: str) -> List[str]:
images (list): list of exact image paths
"""
images = []
image_exts = {".jpg", ".jpeg", ".png"}
pil_exts = {"jpeg", "png"}
for r, _, f in os.walk(path):
for file in f:
exact_path = os.path.join(r, file)
ext_lower = os.path.splitext(exact_path)[-1].lower()
if ext_lower not in {".jpg", ".jpeg", ".png"}:
continue
with Image.open(exact_path) as img: # lazy
if img.format.lower() in {"jpeg", "png"}:
images.append(exact_path)
if os.path.splitext(file)[1].lower() in image_exts:
exact_path = os.path.join(r, file)
with Image.open(exact_path) as img: # lazy
if img.format.lower() in pil_exts:
images.append(exact_path)
return images