Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import os
|
|
|
4 |
|
5 |
def convert_image_format(image, target_format):
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
|
18 |
# Interfaz de Gradio
|
19 |
with gr.Blocks() as demo:
|
20 |
gr.Markdown("### Conversor de formatos de imagen")
|
21 |
-
|
22 |
-
image_input = gr.File(label="Sube tu imagen", file_types=['image'])
|
23 |
-
format_dropdown = gr.Dropdown(label="Selecciona el formato de salida", choices=["jpg", "png", "webp"])
|
24 |
convert_button = gr.Button("Convertir")
|
25 |
-
|
26 |
-
output_gallery = gr.Gallery(label="
|
27 |
-
|
28 |
convert_button.click(fn=convert_image_format, inputs=[image_input, format_dropdown], outputs=output_gallery)
|
29 |
|
30 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
+
import tempfile
|
5 |
|
6 |
def convert_image_format(image, target_format):
|
7 |
+
# Crear un directorio temporal para guardar la imagen
|
8 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
9 |
+
# Definir la ruta de salida
|
10 |
+
output_name = os.path.join(tmpdirname, f"output_image.{target_format.lower()}")
|
11 |
+
|
12 |
+
# Abrir la imagen con PIL
|
13 |
+
img = Image.open(image)
|
14 |
+
|
15 |
+
# Guardar la imagen en el formato deseado
|
16 |
+
img.save(output_name, format=target_format.upper())
|
17 |
+
|
18 |
+
# Devolver la ruta del archivo convertido
|
19 |
+
return output_name
|
20 |
|
21 |
# Interfaz de Gradio
|
22 |
with gr.Blocks() as demo:
|
23 |
gr.Markdown("### Conversor de formatos de imagen")
|
24 |
+
|
25 |
+
image_input = gr.File(label="Sube tu imagen", file_types=['image'])
|
26 |
+
format_dropdown = gr.Dropdown(label="Selecciona el formato de salida", choices=["jpg", "png", "webp"], value="webp")
|
27 |
convert_button = gr.Button("Convertir")
|
28 |
+
|
29 |
+
output_gallery = gr.Gallery(label="Imagen convertida")
|
30 |
+
|
31 |
convert_button.click(fn=convert_image_format, inputs=[image_input, format_dropdown], outputs=output_gallery)
|
32 |
|
33 |
demo.launch()
|