From 6363ea459035c86515187ae76a5c9e94cfa179bf Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Mon, 19 Jan 2026 14:22:24 +0100 Subject: [PATCH] Update engine.py --- engine.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/engine.py b/engine.py index 078cf5f..8abad8a 100644 --- a/engine.py +++ b/engine.py @@ -354,14 +354,25 @@ class SorterEngine: return t_dst, c_dst @staticmethod - def compress_for_web(path, quality): - """Compresses images for UI performance.""" + def compress_for_web(path, quality, target_size=400): + """ + Loads image, RESIZES it to a thumbnail, and returns bytes. + """ try: with Image.open(path) as img: + # 1. Convert to RGB (fixes PNG/Transparency issues) + img = img.convert("RGB") + + # 2. HUGE SPEEDUP: Resize before saving + # We use 'thumbnail' which maintains aspect ratio + img.thumbnail((target_size, target_size), Image.Resampling.LANCZOS) + + # 3. Save to buffer buf = BytesIO() - img.convert("RGB").save(buf, format="JPEG", quality=quality) - return buf - except: return None + img.save(buf, format="JPEG", quality=quality, optimize=True) + return buf.getvalue() # Return bytes directly for caching + except Exception: + return None @staticmethod def revert_action(action):