Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
import os
|
4 |
import tempfile
|
5 |
|
@@ -23,15 +23,22 @@ def optimize_image(image):
|
|
23 |
|
24 |
# 3. Reducción de resolución
|
25 |
reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
|
26 |
-
reduced_img = img.resize((img.width // 2, img.height // 2), 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 |
# Retornar imágenes optimizadas y sus tamaños
|
32 |
return (lossless_img, f"Sin pérdida: {lossless_size:.2f} KB",
|
33 |
lossy_img, f"Con pérdida: {lossy_size:.2f} KB",
|
34 |
-
reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB"
|
|
|
35 |
|
36 |
# Interfaz de Gradio
|
37 |
with gr.Blocks() as demo:
|
@@ -50,6 +57,14 @@ with gr.Blocks() as demo:
|
|
50 |
optimized_output3 = gr.Image(label="Reducción de resolución")
|
51 |
optimized_size3 = gr.Text(label="Tamaño del archivo con reducción de resolución")
|
52 |
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
import os
|
4 |
import tempfile
|
5 |
|
|
|
23 |
|
24 |
# 3. Reducción de resolución
|
25 |
reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
|
26 |
+
reduced_img = img.resize((img.width // 2, img.height // 2), Image.LANCZOS)
|
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:
|
|
|
57 |
optimized_output3 = gr.Image(label="Reducción de resolución")
|
58 |
optimized_size3 = gr.Text(label="Tamaño del archivo con reducción de resolución")
|
59 |
|
60 |
+
optimized_output4 = gr.Image(label="Optimización WebP con pérdida")
|
61 |
+
optimized_size4 = gr.Text(label="Tamaño del archivo WebP con pérdida")
|
62 |
+
|
63 |
+
optimize_button.click(fn=optimize_image, inputs=image_input, outputs=[
|
64 |
+
optimized_output1, optimized_size1,
|
65 |
+
optimized_output2, optimized_size2,
|
66 |
+
optimized_output3, optimized_size3,
|
67 |
+
optimized_output4, optimized_size4
|
68 |
+
])
|
69 |
|
70 |
demo.launch()
|