Spaces:
Sleeping
Sleeping
| 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() | |