created a new yield_images generator function to yield the images in a given path. The functionality is equivalent to list_images, but, instead of building then return a list, it yields the image path at each iteration.

This commit is contained in:
Samuel J. Woodward 2025-01-06 09:08:24 -05:00
parent ca9ecbb3ca
commit b11eec0eab

View File

@ -1,7 +1,7 @@
# built-in dependencies # built-in dependencies
import os import os
import io import io
from typing import List, Union, Tuple from typing import Generator, List, Union, Tuple
import hashlib import hashlib
import base64 import base64
from pathlib import Path from pathlib import Path
@ -35,6 +35,26 @@ def list_images(path: str) -> List[str]:
return images return images
def yield_images(path: str) -> Generator[str]:
"""
List images in a given path
Args:
path (str): path's location
Yields:
image (str): image path
"""
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:
exact_path = os.path.join(r, file)
with Image.open(exact_path) as img: # lazy
if img.format.lower() in pil_exts:
yield exact_path
def find_image_hash(file_path: str) -> str: def find_image_hash(file_path: str) -> str:
""" """
Find the hash of given image file with its properties Find the hash of given image file with its properties