Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|