Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,9 @@ def optimize_image(image):
|
|
7 |
# Leer la imagen original
|
8 |
img = Image.open(image)
|
9 |
|
|
|
|
|
|
|
10 |
# Ruta temporal para guardar las imágenes optimizadas
|
11 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
12 |
# 1. Compresión sin pérdida
|
@@ -14,12 +17,14 @@ def optimize_image(image):
|
|
14 |
img.save(lossless_output_path, format="PNG", optimize=True)
|
15 |
lossless_size = os.path.getsize(lossless_output_path) / 1024 # tamaño en KB
|
16 |
lossless_img = Image.open(lossless_output_path)
|
|
|
17 |
|
18 |
# 2. Compresión con pérdida (reducción de calidad)
|
19 |
lossy_output_path = os.path.join(tmpdirname, "lossy.jpg")
|
20 |
img.save(lossy_output_path, format="JPEG", quality=50, optimize=True)
|
21 |
lossy_size = os.path.getsize(lossy_output_path) / 1024 # tamaño en KB
|
22 |
lossy_img = Image.open(lossy_output_path)
|
|
|
23 |
|
24 |
# 3. Reducción de resolución
|
25 |
reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
|
@@ -27,18 +32,20 @@ def optimize_image(image):
|
|
27 |
reduced_img.save(reduced_output_path, format="JPEG", quality=85, optimize=True)
|
28 |
reduced_size = os.path.getsize(reduced_output_path) / 1024 # tamaño en KB
|
29 |
reduced_img = Image.open(reduced_output_path)
|
|
|
30 |
|
31 |
# 4. Compresión con WebP (con pérdida)
|
32 |
webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp")
|
33 |
img.save(webp_lossy_output_path, format="WEBP", quality=50, optimize=True)
|
34 |
webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024 # tamaño en KB
|
35 |
webp_lossy_img = Image.open(webp_lossy_output_path)
|
|
|
36 |
|
37 |
-
# Retornar imágenes optimizadas y sus tamaños
|
38 |
-
return (lossless_img, f"Sin pérdida: {lossless_size:.2f} KB",
|
39 |
-
lossy_img, f"Con pérdida: {lossy_size:.2f} KB",
|
40 |
-
reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB",
|
41 |
-
webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB")
|
42 |
|
43 |
# Interfaz de Gradio
|
44 |
with gr.Blocks() as demo:
|
|
|
7 |
# Leer la imagen original
|
8 |
img = Image.open(image)
|
9 |
|
10 |
+
# Calcular el tamaño del archivo original
|
11 |
+
original_size = os.path.getsize(image.name) / 1024 # tamaño en KB
|
12 |
+
|
13 |
# Ruta temporal para guardar las imágenes optimizadas
|
14 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
15 |
# 1. Compresión sin pérdida
|
|
|
17 |
img.save(lossless_output_path, format="PNG", optimize=True)
|
18 |
lossless_size = os.path.getsize(lossless_output_path) / 1024 # tamaño en KB
|
19 |
lossless_img = Image.open(lossless_output_path)
|
20 |
+
lossless_diff = original_size - lossless_size
|
21 |
|
22 |
# 2. Compresión con pérdida (reducción de calidad)
|
23 |
lossy_output_path = os.path.join(tmpdirname, "lossy.jpg")
|
24 |
img.save(lossy_output_path, format="JPEG", quality=50, optimize=True)
|
25 |
lossy_size = os.path.getsize(lossy_output_path) / 1024 # tamaño en KB
|
26 |
lossy_img = Image.open(lossy_output_path)
|
27 |
+
lossy_diff = original_size - lossy_size
|
28 |
|
29 |
# 3. Reducción de resolución
|
30 |
reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
|
|
|
32 |
reduced_img.save(reduced_output_path, format="JPEG", quality=85, optimize=True)
|
33 |
reduced_size = os.path.getsize(reduced_output_path) / 1024 # tamaño en KB
|
34 |
reduced_img = Image.open(reduced_output_path)
|
35 |
+
reduced_diff = original_size - reduced_size
|
36 |
|
37 |
# 4. Compresión con WebP (con pérdida)
|
38 |
webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp")
|
39 |
img.save(webp_lossy_output_path, format="WEBP", quality=50, optimize=True)
|
40 |
webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024 # tamaño en KB
|
41 |
webp_lossy_img = Image.open(webp_lossy_output_path)
|
42 |
+
webp_lossy_diff = original_size - webp_lossy_size
|
43 |
|
44 |
+
# Retornar imágenes optimizadas y sus tamaños con la diferencia
|
45 |
+
return (lossless_img, f"Sin pérdida: {lossless_size:.2f} KB (diferencia: {-lossless_diff:.2f} KB)",
|
46 |
+
lossy_img, f"Con pérdida: {lossy_size:.2f} KB (diferencia: {-lossy_diff:.2f} KB)",
|
47 |
+
reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB (diferencia: {-reduced_diff:.2f} KB)",
|
48 |
+
webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB (diferencia: {-webp_lossy_diff:.2f} KB)")
|
49 |
|
50 |
# Interfaz de Gradio
|
51 |
with gr.Blocks() as demo:
|