Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
interface = gr.Interface(
|
| 16 |
-
fn=
|
| 17 |
-
inputs=
|
| 18 |
-
outputs=gr.File(label="Download Compressed Image"),
|
| 19 |
-
title="Image Compressor",
|
| 20 |
-
description="Upload
|
| 21 |
)
|
| 22 |
|
| 23 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def compress_images(*image_paths):
|
| 7 |
+
compressed_files = []
|
| 8 |
+
for image_path in image_paths:
|
| 9 |
+
if image_path is not None:
|
| 10 |
+
img = Image.open(image_path)
|
| 11 |
+
compressed_io = io.BytesIO()
|
| 12 |
+
img.save(compressed_io, format='JPEG', quality=30)
|
| 13 |
+
compressed_io.seek(0)
|
| 14 |
+
compressed_filename = f"compressed_{os.path.basename(image_path)}"
|
| 15 |
+
with open(compressed_filename, "wb") as f:
|
| 16 |
+
f.write(compressed_io.read())
|
| 17 |
+
compressed_files.append(compressed_filename)
|
| 18 |
+
else:
|
| 19 |
+
compressed_files.append(None)
|
| 20 |
+
return compressed_files
|
| 21 |
+
|
| 22 |
+
image_inputs = [gr.Image(type="filepath", label=f"Upload Image {i+1}") for i in range(5)]
|
| 23 |
|
| 24 |
interface = gr.Interface(
|
| 25 |
+
fn=compress_images,
|
| 26 |
+
inputs=image_inputs,
|
| 27 |
+
outputs=[gr.File(label=f"Download Compressed Image {i+1}") for i in range(5)],
|
| 28 |
+
title="Multi-Image Compressor",
|
| 29 |
+
description="Upload up to 5 images to compress them and download the compressed versions."
|
| 30 |
)
|
| 31 |
|
| 32 |
+
interface.launch()
|