cr8 commited on
Commit
b199da5
·
verified ·
1 Parent(s): 0a94cee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -33
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(input_image, output_format):
7
- # Convert image using Pillow (CPU-efficient)
8
- img = Image.open(input_image)
9
- output_buffer = io.BytesIO()
10
 
11
- # Optimize for web formats
12
- if output_format == "JPEG":
13
- img.convert("RGB").save(output_buffer, format=output_format, quality=85)
14
- else:
15
- img.save(output_buffer, format=output_format)
16
 
17
- # Return converted image bytes
18
- return output_buffer.getvalue()
19
 
20
- # Gradio Interface
21
- interface = gr.Interface(
22
- fn=convert_image,
23
- inputs=[
24
- gr.Image(type="filepath", label="Upload Image"),
25
- gr.Dropdown(
26
- ["PNG", "JPEG", "WEBP", "BMP"],
27
- value="PNG",
28
- label="Output Format"
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
- if __name__ == "__main__":
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()