Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
import tempfile
|
|
|
5 |
|
6 |
def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_quality):
|
7 |
img = Image.open(image)
|
@@ -52,91 +53,63 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
|
|
52 |
webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB \n(diferencia: {-webp_lossy_diff:.2f} KB)", webp_lossy_output_path
|
53 |
]
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
download_button1 = gr.File(label="Descargar", visible=True)
|
66 |
-
optimized_size1 = gr.Text(value="", interactive=False, show_label=False)
|
67 |
-
|
68 |
-
with gr.Column():
|
69 |
-
optimized_output2 = gr.Image(label="Optimización con pérdida (JPEG)")
|
70 |
-
jpeg_quality = gr.Slider(label="Calidad JPEG", minimum=10, maximum=100, value=75, step=1)
|
71 |
-
download_button2 = gr.File(label="Descargar", visible=True)
|
72 |
-
optimized_size2 = gr.Text(value="", interactive=False, show_label=False)
|
73 |
-
|
74 |
-
with gr.Column():
|
75 |
-
optimized_output3 = gr.Image(label="Reducción de resolución (JPEG)")
|
76 |
-
jpeg_resolution = gr.Slider(label="Resolución JPEG (%)", minimum=10, maximum=100, value=50, step=1)
|
77 |
-
download_button3 = gr.File(label="Descargar", visible=True)
|
78 |
-
optimized_size3 = gr.Text(value="", interactive=False, show_label=False)
|
79 |
|
80 |
-
with gr.
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
optimized_output2, optimized_size2, download_button2,
|
104 |
-
optimized_output3, optimized_size3, download_button3,
|
105 |
-
optimized_output4, optimized_size4, download_button4
|
106 |
-
]
|
107 |
-
)
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
optimized_output1, optimized_size1, download_button1,
|
125 |
-
optimized_output2, optimized_size2, download_button2,
|
126 |
-
optimized_output3, optimized_size3, download_button3,
|
127 |
-
optimized_output4, optimized_size4, download_button4
|
128 |
-
]
|
129 |
-
)
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
optimized_output2, optimized_size2, download_button2,
|
137 |
-
optimized_output3, optimized_size3, download_button3,
|
138 |
-
optimized_output4, optimized_size4, download_button4
|
139 |
-
]
|
140 |
-
)
|
141 |
|
142 |
demo.launch()
|
|
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
import tempfile
|
5 |
+
from transformers import pipeline
|
6 |
|
7 |
def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_quality):
|
8 |
img = Image.open(image)
|
|
|
53 |
webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB \n(diferencia: {-webp_lossy_diff:.2f} KB)", webp_lossy_output_path
|
54 |
]
|
55 |
|
56 |
+
# Función de ejemplo para aplicar un modelo de super-resolución
|
57 |
+
def super_res_image(image):
|
58 |
+
esrgan = pipeline("image-super-resolution", model="microsoft/real-esrgan")
|
59 |
+
super_res_img = esrgan(image)
|
60 |
+
return super_res_img
|
61 |
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
with gr.Tab("Optimización Tradicional"):
|
64 |
+
image_input = gr.File(label="Sube tu imagen", file_types=['image'])
|
65 |
+
optimize_button = gr.Button("Optimizar")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column():
|
69 |
+
optimized_output1 = gr.Image(label="Optimización sin pérdida")
|
70 |
+
png_optimize = gr.Checkbox(label="Optimizar PNG", value=True)
|
71 |
+
download_button1 = gr.File(label="Descargar", visible=True)
|
72 |
+
optimized_size1 = gr.Text(value="", interactive=False, show_label=False)
|
73 |
|
74 |
+
with gr.Column():
|
75 |
+
optimized_output2 = gr.Image(label="Optimización con pérdida (JPEG)")
|
76 |
+
jpeg_quality = gr.Slider(label="Calidad JPEG", minimum=10, maximum=100, value=50, step=1)
|
77 |
+
download_button2 = gr.File(label="Descargar", visible=True)
|
78 |
+
optimized_size2 = gr.Text(value="", interactive=False, show_label=False)
|
79 |
+
|
80 |
+
with gr.Column():
|
81 |
+
optimized_output3 = gr.Image(label="Reducción de resolución (JPEG)")
|
82 |
+
jpeg_resolution = gr.Slider(label="Resolución JPEG (%)", minimum=10, maximum=100, value=50, step=1)
|
83 |
+
download_button3 = gr.File(label="Descargar", visible=True)
|
84 |
+
optimized_size3 = gr.Text(value="", interactive=False, show_label=False)
|
85 |
|
86 |
+
with gr.Column():
|
87 |
+
optimized_output4 = gr.Image(label="Optimización WebP con pérdida")
|
88 |
+
webp_quality = gr.Slider(label="Calidad WebP", minimum=10, maximum=100, value=50, step=1)
|
89 |
+
download_button4 = gr.File(label="Descargar", visible=True)
|
90 |
+
optimized_size4 = gr.Text(value="", interactive=False, show_label=False)
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
# Conectar cada control a la función de optimización
|
93 |
+
optimize_button.click(
|
94 |
+
fn=optimize_image,
|
95 |
+
inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
|
96 |
+
outputs=[
|
97 |
+
optimized_output1, optimized_size1, download_button1,
|
98 |
+
optimized_output2, optimized_size2, download_button2,
|
99 |
+
optimized_output3, optimized_size3, download_button3,
|
100 |
+
optimized_output4, optimized_size4, download_button4
|
101 |
+
]
|
102 |
+
)
|
103 |
|
104 |
+
with gr.Tab("Optimización con Modelos de Hugging Face"):
|
105 |
+
hf_image_input = gr.File(label="Sube tu imagen para optimización avanzada", file_types=['image'])
|
106 |
+
super_res_output = gr.Image(label="Super-Resolution Result")
|
107 |
+
hf_button = gr.Button("Aplicar Super-Resolution")
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
hf_button.click(
|
110 |
+
fn=super_res_image,
|
111 |
+
inputs=hf_image_input,
|
112 |
+
outputs=super_res_output
|
113 |
+
)
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
demo.launch()
|