Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,8 +24,8 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
|
|
| 24 |
else:
|
| 25 |
img = Image.open(image)
|
| 26 |
|
| 27 |
-
# Convertir la imagen a RGB si est谩 en RGBA
|
| 28 |
-
if img.mode
|
| 29 |
img = img.convert('RGB')
|
| 30 |
|
| 31 |
original_size = os.path.getsize(image.name) / 1024 # tama帽o en KB
|
|
@@ -64,7 +64,21 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Funci贸n para aplicar un modelo seleccionado desde Hugging Face
|
| 70 |
def apply_model(image, model_name):
|
|
@@ -73,7 +87,6 @@ def apply_model(image, model_name):
|
|
| 73 |
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
with gr.Tab("Optimizaci贸n Tradicional"):
|
| 76 |
-
# Aceptar im谩genes y archivos .heic
|
| 77 |
image_input = gr.File(label="Sube tu imagen", file_types=['image', '.heic'])
|
| 78 |
optimize_button = gr.Button("Optimizar")
|
| 79 |
|
|
@@ -131,4 +144,3 @@ with gr.Blocks() as demo:
|
|
| 131 |
)
|
| 132 |
|
| 133 |
demo.launch()
|
| 134 |
-
|
|
|
|
| 24 |
else:
|
| 25 |
img = Image.open(image)
|
| 26 |
|
| 27 |
+
# Convertir la imagen a RGB si est谩 en RGBA o en un modo no soportado por JPEG
|
| 28 |
+
if img.mode not in ['RGB', 'L']:
|
| 29 |
img = img.convert('RGB')
|
| 30 |
|
| 31 |
original_size = os.path.getsize(image.name) / 1024 # tama帽o en KB
|
|
|
|
| 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 = []
|
| 73 |
+
for format_name, path, size, original_size in results:
|
| 74 |
+
if format_name and path:
|
| 75 |
+
img = Image.open(path)
|
| 76 |
+
percent_reduction = 100 * (original_size - size) / original_size
|
| 77 |
+
outputs.extend([img, f"{format_name}: {size:.2f} KB\n(diferencia: {-percent_reduction:.2f} KB)", path])
|
| 78 |
+
else:
|
| 79 |
+
outputs.extend([None, "", None])
|
| 80 |
+
|
| 81 |
+
return outputs
|
| 82 |
|
| 83 |
# Funci贸n para aplicar un modelo seleccionado desde Hugging Face
|
| 84 |
def apply_model(image, model_name):
|
|
|
|
| 87 |
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
with gr.Tab("Optimizaci贸n Tradicional"):
|
|
|
|
| 90 |
image_input = gr.File(label="Sube tu imagen", file_types=['image', '.heic'])
|
| 91 |
optimize_button = gr.Button("Optimizar")
|
| 92 |
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
demo.launch()
|
|
|