Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
def convert_image(
|
| 7 |
-
|
| 8 |
-
img =
|
| 9 |
-
output_buffer = io.BytesIO()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
else:
|
| 15 |
-
img.save(output_buffer, format=output_format)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
return output_buffer.getvalue()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
gr.
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
-
],
|
| 31 |
-
outputs=gr.File(label="Download Converted Image"),
|
| 32 |
-
examples=[
|
| 33 |
-
["example.jpg", "PNG"],
|
| 34 |
-
["example.png", "JPEG"]
|
| 35 |
-
],
|
| 36 |
-
title="Lightweight Image Converter",
|
| 37 |
-
description="Convert images between formats with minimal CPU usage"
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
|
|
|
| 4 |
|
| 5 |
+
def convert_image(image, output_format):
|
| 6 |
+
img = Image.open(image)
|
| 7 |
+
img = img.convert("RGB") # Ensuring compatibility for formats like TIFF
|
|
|
|
| 8 |
|
| 9 |
+
output = io.BytesIO()
|
| 10 |
+
img.save(output, format=output_format.upper())
|
| 11 |
+
output.seek(0)
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
return output
|
|
|
|
| 14 |
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
gr.Markdown("# Simple Image Converter")
|
| 17 |
+
with gr.Row():
|
| 18 |
+
input_image = gr.File(label="Upload Image", type="file")
|
| 19 |
+
output_format = gr.Radio(["JPG", "PNG"], label="Convert To")
|
| 20 |
+
convert_button = gr.Button("Convert")
|
| 21 |
+
output_image = gr.File(label="Download Converted Image")
|
| 22 |
+
|
| 23 |
+
convert_button.click(convert_image, inputs=[input_image, output_format], outputs=output_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
demo.launch()
|
|
|