bigbossmonster commited on
Commit
0613f19
·
verified ·
1 Parent(s): daed24b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
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 compress_advanced(image_bytes, max_width=800, use_webp=True):
 
 
 
 
161
  try:
162
  img = Image.open(io.BytesIO(image_bytes))
163
 
164
- # 1. Resize efficiently (using thumbnail is faster/safer than manual math)
165
  img.thumbnail((max_width, max_width), Image.Resampling.LANCZOS)
166
 
167
  buffer = io.BytesIO()
168
 
 
 
 
 
169
  if use_webp:
170
- # OPTION A: WebP (Best Balance)
171
- # quality=80 in WebP looks like quality=90+ in JPEG but is much smaller
172
- img.save(buffer, format="WEBP", quality=80, method=6)
173
  else:
174
- # OPTION B: Optimized JPEG (If you strictly need JPG)
175
- # Handle transparency (paste on white) only for JPEG
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
- background.paste(img, mask=img.split()[3] if len(img.split()) > 3 else None)
 
 
 
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=80,
188
- optimize=True, # Extra pass to shrink file size
189
- progressive=True, # Loads gradually on slow nets & usually smaller
190
- subsampling=0 # Keeps edges sharp (4:4:4)
191
  )
192
 
193
  return buffer.getvalue()
194
 
195
  except Exception as e:
196
- logger.error(f"Compression failed: {e}")
 
 
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):