From f57db40e4f61759cbf4fa25017059b163ff80941 Mon Sep 17 00:00:00 2001 From: lelo Date: Mon, 31 Mar 2025 09:11:21 +0000 Subject: [PATCH] convert images to jpeg only --- app.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 953a3e1..dbf3015 100755 --- a/app.py +++ b/app.py @@ -222,12 +222,18 @@ def serve_file(subpath): # Image processing branch (with caching) try: with Image.open(full_path) as img: - img.thumbnail((1200, 1200)) - img_bytes = io.BytesIO() - img.save(img_bytes, format='PNG', quality=85) - img_bytes = img_bytes.getvalue() - cache.set(subpath, (img_bytes, mime)) - response = send_file(io.BytesIO(img_bytes), mimetype=mime, conditional=True) + img.thumbnail((1920, 1920)) + if img.mode in ("RGBA", "P"): + img = img.convert("RGB") + output_format = 'JPEG' + output_mime = 'image/jpeg' + save_kwargs = {'quality': 85} + + img_bytes_io = io.BytesIO() + img.save(img_bytes_io, format=output_format, **save_kwargs) + thumb_bytes = img_bytes_io.getvalue() + cache.set(subpath, (thumb_bytes, output_mime)) + response = send_file(io.BytesIO(thumb_bytes), mimetype=output_mime, conditional=True) except Exception as e: app.logger.error(f"Image processing failed for {subpath}: {e}") abort(500)