Merlintxu commited on
Commit
2e03b03
1 Parent(s): 9ad37b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -42,31 +42,24 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
42
  results.append(("PNG", lossless_output_path, lossless_size, original_size))
43
 
44
  # Optimización para JPEG
45
- if jpeg_quality != 50:
46
- lossy_output_path = os.path.join(output_dir, "lossy.jpg")
47
- img.save(lossy_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
48
- lossy_size = os.path.getsize(lossy_output_path) / 1024
49
- results.append(("JPEG", lossy_output_path, lossy_size, original_size))
50
 
51
  # Reducción de resolución (JPEG)
52
- if jpeg_resolution != 50:
53
- reduced_output_path = os.path.join(output_dir, "reduced_resolution.jpg")
54
- new_resolution = (img.width * jpeg_resolution // 100, img.height * jpeg_resolution // 100)
55
- reduced_img = img.resize(new_resolution, Image.LANCZOS)
56
- reduced_img.save(reduced_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
57
- reduced_size = os.path.getsize(reduced_output_path) / 1024
58
- results.append(("JPEG (resolución reducida)", reduced_output_path, reduced_size, original_size))
59
 
60
  # Optimización para WebP
61
- if webp_quality != 50:
62
- webp_lossy_output_path = os.path.join(output_dir, "lossy.webp")
63
- img.save(webp_lossy_output_path, format="WEBP", quality=webp_quality, optimize=True)
64
- webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
65
- results.append(("WebP", webp_lossy_output_path, webp_lossy_size, original_size))
66
-
67
- # Si alguna de las optimizaciones no se ejecutó, devolver valores por defecto para completar los 12 elementos
68
- while len(results) < 4:
69
- results.append(("", "", 0, original_size))
70
 
71
  # Preparar las salidas esperadas
72
  outputs = []
 
42
  results.append(("PNG", lossless_output_path, lossless_size, original_size))
43
 
44
  # Optimización para JPEG
45
+ lossy_output_path = os.path.join(output_dir, "lossy.jpg")
46
+ img.save(lossy_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
47
+ lossy_size = os.path.getsize(lossy_output_path) / 1024
48
+ results.append(("JPEG", lossy_output_path, lossy_size, original_size))
 
49
 
50
  # Reducción de resolución (JPEG)
51
+ reduced_output_path = os.path.join(output_dir, "reduced_resolution.jpg")
52
+ new_resolution = (img.width * jpeg_resolution // 100, img.height * jpeg_resolution // 100)
53
+ reduced_img = img.resize(new_resolution, Image.LANCZOS)
54
+ reduced_img.save(reduced_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
55
+ reduced_size = os.path.getsize(reduced_output_path) / 1024
56
+ results.append(("JPEG (resolución reducida)", reduced_output_path, reduced_size, original_size))
 
57
 
58
  # Optimización para WebP
59
+ webp_lossy_output_path = os.path.join(output_dir, "lossy.webp")
60
+ img.save(webp_lossy_output_path, format="WEBP", quality=webp_quality, optimize=True)
61
+ webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
62
+ results.append(("WebP", webp_lossy_output_path, webp_lossy_size, original_size))
 
 
 
 
 
63
 
64
  # Preparar las salidas esperadas
65
  outputs = []