Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,22 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
with gr.Blocks() as demo:
|
| 16 |
gr.Markdown("# Simple Image Converter")
|
| 17 |
with gr.Row():
|
|
@@ -22,4 +28,4 @@ with gr.Blocks() as demo:
|
|
| 22 |
|
| 23 |
convert_button.click(convert_image, inputs=[input_image, output_format], outputs=output_image)
|
| 24 |
|
| 25 |
-
demo.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
|
| 5 |
+
# Function to convert image
|
| 6 |
+
def convert_image(image_path, output_format):
|
| 7 |
+
try:
|
| 8 |
+
img = Image.open(image_path)
|
| 9 |
+
img = img.convert("RGB") # Ensuring compatibility for formats like TIFF, HEIC, and JFIF
|
| 10 |
+
|
| 11 |
+
output = io.BytesIO()
|
| 12 |
+
save_format = "JPEG" if output_format.upper() == "JPG" else "PNG"
|
| 13 |
+
img.save(output, format=save_format)
|
| 14 |
+
output.seek(0)
|
| 15 |
+
|
| 16 |
+
return output
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return str(e)
|
| 19 |
|
| 20 |
+
# Gradio UI
|
| 21 |
with gr.Blocks() as demo:
|
| 22 |
gr.Markdown("# Simple Image Converter")
|
| 23 |
with gr.Row():
|
|
|
|
| 28 |
|
| 29 |
convert_button.click(convert_image, inputs=[input_image, output_format], outputs=output_image)
|
| 30 |
|
| 31 |
+
demo.launch()
|