Politrees commited on
Commit
f1e910e
·
verified ·
1 Parent(s): f9d6348

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -6
app.py CHANGED
@@ -352,23 +352,104 @@ def merge_audio_files_ffmpeg(input_files, output_file, gap_duration):
352
  os.remove(list_file)
353
 
354
  def convert_image_pillow(input_file, output_path, output_ext):
355
- """Optimized image conversion"""
356
  try:
357
  pil_format = pil_format_for_ext(output_ext)
358
  if not pil_format:
359
  return False
360
 
361
  with Image.open(input_file) as img:
 
 
 
362
  if pil_format == "JPEG":
363
- img = img.convert("RGB")
364
- img.save(output_path, format=pil_format, quality=90, optimize=True)
 
 
 
 
 
 
 
 
 
 
 
365
  elif pil_format == "PNG":
366
- img.save(output_path, format=pil_format, optimize=True, compress_level=6)
367
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
  img.save(output_path, format=pil_format)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
 
370
  return True
371
- except Exception:
 
372
  return False
373
 
374
  def process_file_parallel(args):
 
352
  os.remove(list_file)
353
 
354
  def convert_image_pillow(input_file, output_path, output_ext):
355
+ """Image conversion with maximum quality"""
356
  try:
357
  pil_format = pil_format_for_ext(output_ext)
358
  if not pil_format:
359
  return False
360
 
361
  with Image.open(input_file) as img:
362
+ # Preserve original mode when possible
363
+ original_mode = img.mode
364
+
365
  if pil_format == "JPEG":
366
+ # JPEG doesn't support transparency
367
+ if original_mode in ('RGBA', 'LA', 'P'):
368
+ if original_mode == 'P':
369
+ img = img.convert('RGBA')
370
+ background = Image.new('RGB', img.size, (255, 255, 255))
371
+ if img.mode in ('RGBA', 'LA'):
372
+ background.paste(img, mask=img.split()[-1])
373
+ img = background
374
+ else:
375
+ img = img.convert("RGB")
376
+ # Maximum quality: quality=100, no subsampling
377
+ img.save(output_path, format=pil_format, quality=100, subsampling=0)
378
+
379
  elif pil_format == "PNG":
380
+ # Minimal compression for maximum quality
381
+ img.save(output_path, format=pil_format, compress_level=1)
382
+
383
+ elif pil_format == "WEBP":
384
+ # WebP lossless mode for maximum quality
385
+ img.save(output_path, format=pil_format, lossless=True, quality=100, method=6)
386
+
387
+ elif pil_format == "ICO":
388
+ # ICO - preserve original size and convert to RGBA
389
+ if original_mode not in ('RGBA', 'RGB'):
390
+ img = img.convert('RGBA')
391
+ # Save with original dimensions
392
+ img.save(output_path, format=pil_format, sizes=[(img.width, img.height)])
393
+
394
+ elif pil_format == "BMP":
395
+ # BMP doesn't support transparency
396
+ if original_mode in ('RGBA', 'LA', 'P'):
397
+ if original_mode == 'P':
398
+ img = img.convert('RGBA')
399
+ if img.mode in ('RGBA', 'LA'):
400
+ background = Image.new('RGB', img.size, (255, 255, 255))
401
+ background.paste(img, mask=img.split()[-1])
402
+ img = background
403
+ else:
404
+ img = img.convert('RGB')
405
+ else:
406
+ img = img.convert('RGB')
407
  img.save(output_path, format=pil_format)
408
+
409
+ elif pil_format == "TIFF":
410
+ # TIFF without compression for maximum quality
411
+ img.save(output_path, format=pil_format, compression=None)
412
+
413
+ elif pil_format == "GIF":
414
+ # GIF with high quality palette
415
+ if original_mode == 'RGBA':
416
+ # Convert with adaptive palette for best quality
417
+ alpha = img.split()[-1]
418
+ img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
419
+ # Add transparency
420
+ img.paste(255, alpha.point(lambda x: 0 if x < 128 else 255))
421
+ img.info['transparency'] = 255
422
+ elif original_mode != 'P':
423
+ img = img.convert('P', palette=Image.ADAPTIVE, colors=256)
424
+ img.save(output_path, format=pil_format, optimize=False)
425
+
426
+ elif pil_format in ("PPM", "PGM", "PBM"):
427
+ # PPM formats - raw format for maximum quality
428
+ if pil_format == "PPM":
429
+ img = img.convert('RGB')
430
+ elif pil_format == "PGM":
431
+ img = img.convert('L')
432
+ elif pil_format == "PBM":
433
+ img = img.convert('1')
434
+ img.save(output_path, format="PPM")
435
+
436
+ elif pil_format == "TGA":
437
+ # TGA with RLE disabled for maximum quality
438
+ if original_mode not in ('RGBA', 'RGB'):
439
+ img = img.convert('RGBA')
440
+ img.save(output_path, format=pil_format, compression=None)
441
+
442
+ else:
443
+ # For all other formats, try to save with maximum quality
444
+ try:
445
+ img.save(output_path, format=pil_format, quality=100)
446
+ except TypeError:
447
+ # If quality parameter not supported, save without it
448
+ img.save(output_path, format=pil_format)
449
 
450
  return True
451
+ except Exception as e:
452
+ print(f"Image conversion error: {e}")
453
  return False
454
 
455
  def process_file_parallel(args):