Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
results.append(("JPEG", lossy_output_path, lossy_size, original_size))
|
50 |
|
51 |
# Reducción de resolución (JPEG)
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
results.append(("JPEG (resolución reducida)", reduced_output_path, reduced_size, original_size))
|
59 |
|
60 |
# Optimización para WebP
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
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 = []
|