This commit is contained in:
2026-01-19 14:27:25 +01:00
parent 8328e4d3b4
commit e7144eb6cf

View File

@@ -354,23 +354,27 @@ class SorterEngine:
return t_dst, c_dst return t_dst, c_dst
@staticmethod @staticmethod
def compress_for_web(path, quality, target_size=400): def compress_for_web(path, quality, target_size=None):
""" """
Loads image, RESIZES it to a thumbnail, and returns bytes. Loads image, resizes smart, and saves as WebP.
""" """
try: try:
with Image.open(path) as img: with Image.open(path) as img:
# 1. Convert to RGB (fixes PNG/Transparency issues) # 1. Convert to RGB (WebP handles RGBA, but RGB is safer for consistency)
if img.mode not in ('RGB', 'RGBA'):
img = img.convert("RGB") img = img.convert("RGB")
# 2. HUGE SPEEDUP: Resize before saving # 2. Smart Resize (Only if target_size is provided)
# We use 'thumbnail' which maintains aspect ratio if target_size:
# Only resize if the original is actually bigger
if img.width > target_size or img.height > target_size:
img.thumbnail((target_size, target_size), Image.Resampling.LANCZOS) img.thumbnail((target_size, target_size), Image.Resampling.LANCZOS)
# 3. Save to buffer # 3. Save as WebP
buf = BytesIO() buf = BytesIO()
img.save(buf, format="JPEG", quality=quality, optimize=True) # WebP is faster to decode in browser and smaller on disk
return buf.getvalue() # Return bytes directly for caching img.save(buf, format="WEBP", quality=quality)
return buf.getvalue()
except Exception: except Exception:
return None return None