Spaces:
Running
Running
File size: 1,160 Bytes
9136b4d 16fe620 9136b4d d6e8088 16fe620 d6e8088 16fe620 65ac239 d6e8088 9136b4d d6e8088 b199da5 65ac239 b199da5 8a17642 b199da5 65ac239 b199da5 65ac239 9136b4d 65ac239 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import gradio as gr
from PIL import Image
import io
import tempfile
# Function to convert image
def convert_image(image_path, output_format):
try:
img = Image.open(image_path)
img = img.convert("RGB") # Ensuring compatibility for various formats
save_format = "JPEG" if output_format.upper() == "JPG" else "PNG"
# Save the converted image as a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f".{save_format.lower()}")
img.save(temp_file.name, format=save_format)
return temp_file.name # Returning the file path instead of BytesIO
except Exception as e:
return str(e)
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# Simple Image Converter")
with gr.Row():
input_image = gr.File(label="Upload Image", type="filepath")
output_format = gr.Radio(["JPG", "PNG"], label="Convert To")
convert_button = gr.Button("Convert")
output_image = gr.File(label="Download Converted Image")
convert_button.click(convert_image, inputs=[input_image, output_format], outputs=output_image)
demo.launch()
|