From bd2f20188f2f903da85e4bd4c091462d7bfb0270 Mon Sep 17 00:00:00 2001 From: Flo Greistorfer Date: Tue, 2 Jul 2024 07:56:00 +0200 Subject: [PATCH] switched to Pillow, as it is way faster (4s vs 12s) --- README.md | 5 ++--- generate_html.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 14a23f5..4723417 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,14 @@ - `numpy` library - `tqdm` library - `Jinja2` library -- `ImageMagick` +- `Pillow` library ## Installation Install the required libraries using pip: ```sh -pip install numpy tqdm Jinja2 +pip install numpy tqdm Jinja2 Pillow ``` ## Usage @@ -91,7 +91,6 @@ To exclude specific folders and specify file extensions: ## Notes - The root and web root paths must point to the same folder, one on the filesystem and one on the web server. Use absolute paths. -- Ensure that ImageMagick is installed and accessible in your system for thumbnail generation. - The script generates the preview thumbnails in a `.thumbnails` subdirectory within the root folder. - The `.lock` file prevents multiple instances of the script from running simultaneously. Make sure to remove it if the script terminates unexpectedly. diff --git a/generate_html.py b/generate_html.py index 604c1eb..14da109 100755 --- a/generate_html.py +++ b/generate_html.py @@ -9,6 +9,7 @@ from typing import Any, Dict, List, Optional, Tuple import numpy as np from jinja2 import Environment, FileSystemLoader from tqdm.auto import tqdm +from PIL import Image import cclicense @@ -19,7 +20,7 @@ FAVICON_PATH = ".static/favicon.ico" GLOBAL_CSS_PATH = ".static/global.css" DEFAULT_THEME_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "themes", "default.css") DEFAULT_AUTHOR = "Author" -VERSION = "1.5" +VERSION = "1.6" RAW_EXTENSIONS = [".3fr", ".ari", ".arw", ".bay", ".braw", ".crw", ".cr2", ".cr3", ".cap", ".data", ".dcs", ".dcr", ".dng", ".drf", ".eip", ".erf", ".fff", ".gpr", ".iiq", ".k25", ".kdc", ".mdc", ".mef", ".mos", ".mrw", ".nef", ".nrw", ".obm", ".orf", ".pef", ".ptx", ".pxn", ".r3d", ".raf", ".raw", ".rwl", ".rw2", ".rwz", ".sr2", ".srf", ".srw", ".tif", ".tiff", ".x3f"] IMG_EXTENSIONS = [".jpg", ".jpeg"] EXCLUDES = [".lock", "index.html", ".thumbnails", ".static"] @@ -102,10 +103,13 @@ def generate_thumbnail(arguments: Tuple[str, str]) -> None: folder, item = arguments path = os.path.join(args.root_directory, ".thumbnails", folder.removeprefix(args.root_directory), os.path.splitext(item)[0]) + ".jpg" if not os.path.exists(path) or args.regenerate_thumbnails: - if shutil.which("magick"): - os.system(f'magick "{os.path.join(folder, item)}" -quality 75% -define jpeg:size=1024x1024 -define jpeg:extent=100kb -thumbnail 512x512 -auto-orient "{path}"') - else: - os.system(f'convert "{os.path.join(folder, item)}" -quality 75% -define jpeg:size=1024x1024 -define jpeg:extent=100kb -thumbnail 512x512 -auto-orient "{path}"') + try: + with Image.open(os.path.join(folder, item)) as img: + img.thumbnail((512, 512)) + img.save(path, "JPEG", quality=75, optimize=True) + except OSError: + print(f"Failed to generate thumbnail for {os.path.join(folder, item)}") + pass def get_total_folders(folder: str) -> None: