From ad3ced80dfffd7f63a7e16f2fc739b4a67ef70b9 Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Sun, 4 Aug 2024 12:06:54 -0700 Subject: [PATCH] quick mode - skip thumbnail generation if files already exist --- photoalbum/cli.py | 123 ++++++++++++++++++++++------------------- photoalbum/config.py | 4 ++ photoalbum/generate.py | 18 +++--- 3 files changed, 79 insertions(+), 66 deletions(-) diff --git a/photoalbum/cli.py b/photoalbum/cli.py index e37aa66..8a1376a 100644 --- a/photoalbum/cli.py +++ b/photoalbum/cli.py @@ -31,11 +31,76 @@ def main() -> None: case "init": cmd_init(args) case "generate": + if args.quick: + config.quick = args.quick cmd_generate(args, config) case "clean": cmd_clean(args, config) +def parse_args() -> Namespace: + parser = ArgumentParser() + parser.add_argument( + "--config", + "-c", + default=DEFAULT_CONFIG_PATH, + help="Path to photoalbum.config.json for the album", + ) + parser.add_argument( + "--logging", + default="warning", + choices=[level.lower() for level in logging.getLevelNamesMapping().keys()], + help="Log level", + ) + + subcommands = parser.add_subparsers(title="subcommands") + + init_cmd = subcommands.add_parser( + "init", + help="Initialize an photo directory", + ) + init_cmd.set_defaults(action="init") + init_cmd.add_argument( + "album_path", + nargs="?", + default=".", + help="Path to the main photos directory", + ) + + # Generate subcommand + generate_cmd = subcommands.add_parser( + "generate", + help="Generate the HTML photo album", + ) + generate_cmd.set_defaults(action="generate") + generate_cmd.add_argument( + "--quick", + action="store_true", + help="Quick mode - don't regenerate thumbnails", + ) + generate_cmd.add_argument( + "album_path", + nargs="?", + default=".", + help="Path to the main photos directory", + ) + + # Clean subcommand + clean_cmd = subcommands.add_parser( + "clean", + help="Remove all generated content from the photo album directory", + ) + clean_cmd.set_defaults(action="clean") + clean_cmd.add_argument( + "album_path", + nargs="?", + default=".", + help="Path to the main photos directory", + ) + + return parser.parse_args() + + ######################################## # Command functions def cmd_init(args: Namespace) -> None: @@ -86,64 +151,6 @@ def cmd_clean(args: Namespace, config: Config) -> None: ######################################## # CLI Util functions -def parse_args() -> Namespace: - parser = ArgumentParser() - parser.add_argument( - "--config", - "-c", - default=DEFAULT_CONFIG_PATH, - help="Path to photoalbum.config.json for the album", - ) - parser.add_argument( - "--logging", - default="warning", - choices=[level.lower() for level in logging.getLevelNamesMapping().keys()], - help="Log level", - ) - - subcommands = parser.add_subparsers(title="subcommands") - - init_cmd = subcommands.add_parser( - "init", - help="Initialize an photo directory", - ) - init_cmd.set_defaults(action="init") - init_cmd.add_argument( - "album_path", - nargs="?", - default=".", - help="Path to the main photos directory", - ) - - # Generate subcommand - generate_cmd = subcommands.add_parser( - "generate", - help="Generate the HTML photo album", - ) - generate_cmd.set_defaults(action="generate") - generate_cmd.add_argument( - "album_path", - nargs="?", - default=".", - help="Path to the main photos directory", - ) - - # Clean subcommand - clean_cmd = subcommands.add_parser( - "clean", - help="Remove all generated content from the photo album directory", - ) - clean_cmd.set_defaults(action="clean") - clean_cmd.add_argument( - "album_path", - nargs="?", - default=".", - help="Path to the main photos directory", - ) - - return parser.parse_args() - - def setup_logging(level_str: str) -> None: levels = logging.getLevelNamesMapping() level = levels[level_str.upper()] diff --git a/photoalbum/config.py b/photoalbum/config.py index 6187895..82e8ec4 100644 --- a/photoalbum/config.py +++ b/photoalbum/config.py @@ -16,6 +16,10 @@ class Config: # Size of the image when looking at the standalone image page view_size: tuple[int, int] = (1920, 1080) + # Quick mode: + # - Don't regenerate thumbnails if they already exist + quick: bool = False + @classmethod def from_yaml(cls, contents: bytes) -> "Config": conf = cls() diff --git a/photoalbum/generate.py b/photoalbum/generate.py index a5e2191..36fdaca 100644 --- a/photoalbum/generate.py +++ b/photoalbum/generate.py @@ -149,17 +149,19 @@ def generate_thumbnails(config: Config, root_dir: ImageDirectory) -> None: slides_path = image_path.path.parent / "slides" slides_path.mkdir(exist_ok=True) - thumb_img = orig_img.copy() - thumb_img.thumbnail(config.thumbnail_size) thumb_path = image_path.thumbnail_path() - thumb_img.save(thumb_path) - logger.info(f'Generated thumbnail size "{image_path.path}" -> "{thumb_path}"') + if not thumb_path.exists() or not config.quick: + thumb_img = orig_img.copy() + thumb_img.thumbnail(config.thumbnail_size) + thumb_img.save(thumb_path) + logger.info(f'Generated thumbnail size "{image_path.path}" -> "{thumb_path}"') - screen_img = orig_img.copy() - screen_img.thumbnail(config.view_size) screen_path = image_path.display_path() - screen_img.save(screen_path) - logger.info(f'Generated screen size "{image_path.path}" -> "{screen_path}"') + if not screen_path.exists() or not config.quick: + screen_img = orig_img.copy() + screen_img.thumbnail(config.view_size) + screen_img.save(screen_path) + logger.info(f'Generated screen size "{image_path.path}" -> "{screen_path}"') def generate_html(config: Config, root_dir: ImageDirectory) -> None: