Spaces:
Running
Running
import gradio as gr | |
from PIL import Image | |
import os | |
import tempfile | |
def convert_image_format(image, target_format): | |
# Crear un directorio temporal para guardar la imagen | |
with tempfile.TemporaryDirectory() as tmpdirname: | |
# Definir la ruta de salida | |
output_name = os.path.join(tmpdirname, f"output_image.{target_format.lower()}") | |
# Abrir la imagen con PIL | |
img = Image.open(image) | |
# Guardar la imagen en el formato deseado | |
img.save(output_name, format=target_format.upper()) | |
# Devolver la ruta del archivo convertido | |
return output_name | |
# Interfaz de Gradio | |
with gr.Blocks() as demo: | |
gr.Markdown("### Conversor de formatos de imagen") | |
image_input = gr.File(label="Sube tu imagen", file_types=['image']) | |
format_dropdown = gr.Dropdown(label="Selecciona el formato de salida", choices=["jpg", "png", "webp"], value="webp") | |
convert_button = gr.Button("Convertir") | |
output_gallery = gr.Gallery(label="Imagen convertida") | |
convert_button.click(fn=convert_image_format, inputs=[image_input, format_dropdown], outputs=output_gallery) | |
demo.launch() | |