Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -154,46 +154,56 @@ def parse_srt(content: str):
|
|
| 154 |
return parsed
|
| 155 |
|
| 156 |
|
| 157 |
-
|
| 158 |
logger = logging.getLogger(__name__)
|
| 159 |
|
| 160 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
try:
|
| 162 |
img = Image.open(io.BytesIO(image_bytes))
|
| 163 |
|
| 164 |
-
# 1. Resize
|
| 165 |
img.thumbnail((max_width, max_width), Image.Resampling.LANCZOS)
|
| 166 |
|
| 167 |
buffer = io.BytesIO()
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
if use_webp:
|
| 170 |
-
#
|
| 171 |
-
|
| 172 |
-
img.save(buffer, format="WEBP", quality=80, method=6)
|
| 173 |
else:
|
| 174 |
-
#
|
| 175 |
-
# Handle transparency (paste on white)
|
| 176 |
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
| 177 |
background = Image.new('RGB', img.size, (255, 255, 255))
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
| 179 |
img = background
|
| 180 |
elif img.mode != 'RGB':
|
| 181 |
img = img.convert('RGB')
|
| 182 |
|
| 183 |
-
# Advanced JPEG Optimization
|
| 184 |
img.save(
|
| 185 |
buffer,
|
| 186 |
format="JPEG",
|
| 187 |
-
quality=
|
| 188 |
-
optimize=True,
|
| 189 |
-
progressive=True,
|
| 190 |
-
subsampling=0
|
| 191 |
)
|
| 192 |
|
| 193 |
return buffer.getvalue()
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
-
logger.error(f"
|
|
|
|
|
|
|
| 197 |
return None
|
| 198 |
|
| 199 |
def process_batch_gemini(api_key, items, model_name):
|
|
|
|
| 154 |
return parsed
|
| 155 |
|
| 156 |
|
|
|
|
| 157 |
logger = logging.getLogger(__name__)
|
| 158 |
|
| 159 |
+
def compress_image(image_bytes, max_width=800, quality=80):
|
| 160 |
+
"""
|
| 161 |
+
Compresses an image to WebP (best) or optimized JPEG.
|
| 162 |
+
Renamed back to 'compress_image' to fix your error.
|
| 163 |
+
"""
|
| 164 |
try:
|
| 165 |
img = Image.open(io.BytesIO(image_bytes))
|
| 166 |
|
| 167 |
+
# 1. Efficient Resize (Using thumbnail prevents upscaling artifacts)
|
| 168 |
img.thumbnail((max_width, max_width), Image.Resampling.LANCZOS)
|
| 169 |
|
| 170 |
buffer = io.BytesIO()
|
| 171 |
|
| 172 |
+
# 2. Try WebP first (Best quality/size ratio)
|
| 173 |
+
# If you strictly need JPEG, change use_webp to False
|
| 174 |
+
use_webp = True
|
| 175 |
+
|
| 176 |
if use_webp:
|
| 177 |
+
# method=6 is the strongest compression algo for WebP
|
| 178 |
+
img.save(buffer, format="WEBP", quality=quality, method=6)
|
|
|
|
| 179 |
else:
|
| 180 |
+
# Fallback: Optimized JPEG
|
| 181 |
+
# Handle transparency (paste on white)
|
| 182 |
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
| 183 |
background = Image.new('RGB', img.size, (255, 255, 255))
|
| 184 |
+
# Handle paletted images with transparency
|
| 185 |
+
if img.mode == 'P':
|
| 186 |
+
img = img.convert('RGBA')
|
| 187 |
+
background.paste(img, mask=img.split()[3])
|
| 188 |
img = background
|
| 189 |
elif img.mode != 'RGB':
|
| 190 |
img = img.convert('RGB')
|
| 191 |
|
|
|
|
| 192 |
img.save(
|
| 193 |
buffer,
|
| 194 |
format="JPEG",
|
| 195 |
+
quality=quality,
|
| 196 |
+
optimize=True,
|
| 197 |
+
progressive=True,
|
| 198 |
+
subsampling=0
|
| 199 |
)
|
| 200 |
|
| 201 |
return buffer.getvalue()
|
| 202 |
|
| 203 |
except Exception as e:
|
| 204 |
+
logger.error(f"Image compression failed: {e}")
|
| 205 |
+
# If logging isn't setup, print the error so you can see it
|
| 206 |
+
print(f"Error: {e}")
|
| 207 |
return None
|
| 208 |
|
| 209 |
def process_batch_gemini(api_key, items, model_name):
|