gdo commited on
Commit
aed943a
·
verified ·
1 Parent(s): 082d2d5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import io
4
+
5
+ def optimize_image(image, quality=85):
6
+ """
7
+ Compresses and optimizes an uploaded image for web use.
8
+ """
9
+ # Convert to RGB to avoid issues with PNG transparency
10
+ image = image.convert("RGB")
11
+
12
+ # Save optimized image to buffer
13
+ buffer = io.BytesIO()
14
+ image.save(buffer, format="JPEG", quality=quality, optimize=True)
15
+ buffer.seek(0)
16
+
17
+ return Image.open(buffer)
18
+
19
+ # Gradio Interface
20
+ demo = gr.Interface(
21
+ fn=optimize_image,
22
+ inputs=[
23
+ gr.Image(type="pil", label="Upload Image"),
24
+ gr.Slider(minimum=10, maximum=100, value=85, label="Quality")
25
+ ],
26
+ outputs=gr.Image(type="pil", label="Optimized Image"),
27
+ title="AI-Powered Image Optimizer",
28
+ description="Upload an image to compress and optimize it for better web performance.",
29
+ )
30
+
31
+ demo.launch()