image_exts and pil_exts are now global variables and are now named as IMAGE_EXTS and PIL_EXTS to match Python naming conventions.

This commit is contained in:
Samuel J. Woodward 2025-01-07 10:25:40 -05:00
parent 6a7505269c
commit 83031a427d

View File

@ -14,6 +14,10 @@ from PIL import Image
from werkzeug.datastructures import FileStorage
IMAGE_EXTS = {".jpg", ".jpeg", ".png"}
PIL_EXTS = {"jpeg", "png"}
def list_images(path: str) -> List[str]:
"""
List images in a given path
@ -23,14 +27,12 @@ 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:
if os.path.splitext(file)[1].lower() in image_exts:
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:
if img.format.lower() in PIL_EXTS:
images.append(exact_path)
return images
@ -43,14 +45,12 @@ def yield_images(path: str) -> Generator[str, None, None]:
Yields:
image (str): image path
"""
image_exts = {".jpg", ".jpeg", ".png"}
pil_exts = {"jpeg", "png"}
for r, _, f in os.walk(path):
for file in f:
if os.path.splitext(file)[1].lower() in image_exts:
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:
if img.format.lower() in PIL_EXTS:
yield exact_path