From 71511f2f166e69b50f52c93212fcb07bc8e620f1 Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Sat, 3 Aug 2024 09:49:55 -0700 Subject: [PATCH] move photo finding to its own function for reuse --- .gitignore | 1 + photoalbum/generate.py | 66 +++++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index bdec378..ec74aa9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ # Project specific files test_album DESIGN.md +TODO.md diff --git a/photoalbum/generate.py b/photoalbum/generate.py index 53c2ca1..d09cb1d 100644 --- a/photoalbum/generate.py +++ b/photoalbum/generate.py @@ -13,50 +13,58 @@ def generate(config: Config, album_path: Path) -> None: """ Main generation function """ - generate_thumbnails(config, album_path) - # generate_html(config, album_path) + photos = find_photos(album_path) + generate_thumbnails(config, photos) + generate_html(config, album_path) -def generate_thumbnails(config: Config, path: Path) -> None: +def find_photos(path: Path) -> list[Path]: + """ """ + photos = [] + for parent_path, dirnames, filenames in path.walk(): + if parent_path.name == "slides": + continue + + for filename in filenames: + file_path = parent_path / filename + if is_photo(file_path): + photos.append(file_path) + + return photos + + +def is_photo(photo_path: Path) -> bool: + """ + Returns True if PIL thinks the file is a photo + """ + try: + Image.open(photo_path) + return True + except UnidentifiedImageError: + return False + + +def generate_thumbnails(config: Config, photos: list[Path]) -> None: """ Find all of the images and generate thumbnails and on-screen versions """ - images: list[tuple[Path, str]] = [] - for parent_path, dirnames, filenames in path.walk(): - for filename in filenames: - try: - Image.open(parent_path / filename) - except UnidentifiedImageError: - continue # Not an image file + for photo_path in track(photos, description="Making thumbnails..."): + orig_img = Image.open(photo_path) - # If we modify dirnames in-place, walk() will skip anything we remove - if "slides" in dirnames: - dirnames.remove("slides") - - images.append((parent_path, filename)) - - for parent_path, filename in track(images, description="Making thumbnails..."): - file_path = parent_path / filename - orig_img = Image.open(parent_path / filename) - - slides_path = parent_path / "slides" + slides_path = photo_path.parent / "slides" slides_path.mkdir(exist_ok=True) thumb_img = orig_img.copy() thumb_img.thumbnail(config.thumbnail_size) - thumb_filename = file_path.stem + ".thumb" + file_path.suffix + thumb_filename = photo_path.stem + ".thumb" + photo_path.suffix thumb_img.save(slides_path / thumb_filename) - logger.info( - f"Generated thumbnail size {parent_path / filename} -> {thumb_filename}" - ) + logger.info(f"Generated thumbnail size {photo_path} -> {thumb_filename}") screen_img = orig_img.copy() screen_img.thumbnail(config.view_size) - screen_filename = file_path.stem + ".screen" + file_path.suffix + screen_filename = photo_path.stem + ".screen" + photo_path.suffix screen_img.save(slides_path / screen_filename) - logger.info( - f"Generated screen size {parent_path / filename} -> {screen_filename}" - ) + logger.info(f"Generated screen size {photo_path} -> {screen_filename}") def generate_html(config: Config, path: Path) -> None: