File size: 856 Bytes
aed943a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
from PIL import Image
import io

def optimize_image(image, quality=85):
    """
    Compresses and optimizes an uploaded image for web use.
    """
    # Convert to RGB to avoid issues with PNG transparency
    image = image.convert("RGB")

    # Save optimized image to buffer
    buffer = io.BytesIO()
    image.save(buffer, format="JPEG", quality=quality, optimize=True)
    buffer.seek(0)

    return Image.open(buffer)

# Gradio Interface
demo = gr.Interface(
    fn=optimize_image,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Slider(minimum=10, maximum=100, value=85, label="Quality")
    ],
    outputs=gr.Image(type="pil", label="Optimized Image"),
    title="AI-Powered Image Optimizer",
    description="Upload an image to compress and optimize it for better web performance.",
)

demo.launch()