Update engine.py
This commit is contained in:
21
engine.py
21
engine.py
@@ -354,14 +354,25 @@ class SorterEngine:
|
|||||||
return t_dst, c_dst
|
return t_dst, c_dst
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def compress_for_web(path, quality):
|
def compress_for_web(path, quality, target_size=400):
|
||||||
"""Compresses images for UI performance."""
|
"""
|
||||||
|
Loads image, RESIZES it to a thumbnail, and returns bytes.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
with Image.open(path) as img:
|
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()
|
buf = BytesIO()
|
||||||
img.convert("RGB").save(buf, format="JPEG", quality=quality)
|
img.save(buf, format="JPEG", quality=quality, optimize=True)
|
||||||
return buf
|
return buf.getvalue() # Return bytes directly for caching
|
||||||
except: return None
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def revert_action(action):
|
def revert_action(action):
|
||||||
|
|||||||
Reference in New Issue
Block a user