Merlintxu commited on
Commit
3c18915
1 Parent(s): 18d8275

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -79
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
- with gr.Blocks() as demo:
56
- gr.Markdown("### Optimización de imágenes para la web")
57
-
58
- image_input = gr.File(label="Sube tu imagen", file_types=['image'])
59
- optimize_button = gr.Button("Optimizar")
60
 
61
- with gr.Row():
62
- with gr.Column():
63
- optimized_output1 = gr.Image(label="Optimización sin pérdida")
64
- png_optimize = gr.Checkbox(label="Optimizar PNG", value=True)
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.Column():
81
- optimized_output4 = gr.Image(label="Optimización WebP con pérdida")
82
- webp_quality = gr.Slider(label="Calidad WebP", minimum=10, maximum=100, value=75, step=1)
83
- download_button4 = gr.File(label="Descargar", visible=True)
84
- optimized_size4 = gr.Text(value="", interactive=False, show_label=False)
 
85
 
86
- # Conectar cada control a la función de optimización
87
- optimize_button.click(
88
- fn=optimize_image,
89
- inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
90
- outputs=[
91
- optimized_output1, optimized_size1, download_button1,
92
- optimized_output2, optimized_size2, download_button2,
93
- optimized_output3, optimized_size3, download_button3,
94
- optimized_output4, optimized_size4, download_button4
95
- ]
96
- )
97
 
98
- png_optimize.change(
99
- fn=optimize_image,
100
- inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
101
- outputs=[
102
- optimized_output1, optimized_size1, download_button1,
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
- jpeg_quality.change(
110
- fn=optimize_image,
111
- inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
112
- outputs=[
113
- optimized_output1, optimized_size1, download_button1,
114
- optimized_output2, optimized_size2, download_button2,
115
- optimized_output3, optimized_size3, download_button3,
116
- optimized_output4, optimized_size4, download_button4
117
- ]
118
- )
 
119
 
120
- jpeg_resolution.change(
121
- fn=optimize_image,
122
- inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
123
- outputs=[
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
- webp_quality.change(
132
- fn=optimize_image,
133
- inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
134
- outputs=[
135
- optimized_output1, optimized_size1, download_button1,
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()