| 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. |
| """ |
| |
| image = image.convert("RGB") |
|
|
| |
| buffer = io.BytesIO() |
| image.save(buffer, format="JPEG", quality=quality, optimize=True) |
| buffer.seek(0) |
|
|
| return Image.open(buffer) |
|
|
| |
| 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() |