Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ from PIL import Image, ImageChops, ImageFilter
|
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
import zipfile
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def remove_background(image):
|
| 8 |
"""Remove background using rembg"""
|
|
@@ -28,28 +30,31 @@ def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
|
|
| 28 |
def process_images(image_files):
|
| 29 |
processed = []
|
| 30 |
for img_file in image_files:
|
| 31 |
-
img = Image.open(img_file).convert("RGBA")
|
| 32 |
no_bg = remove_background(img)
|
| 33 |
sticker = add_outline(no_bg)
|
| 34 |
processed.append(sticker)
|
| 35 |
|
| 36 |
-
# Return single PNG or ZIP
|
| 37 |
if len(processed) == 1:
|
|
|
|
| 38 |
buf = io.BytesIO()
|
| 39 |
processed[0].save(buf, format='PNG')
|
| 40 |
-
|
|
|
|
| 41 |
else:
|
|
|
|
| 42 |
zip_buf = io.BytesIO()
|
| 43 |
with zipfile.ZipFile(zip_buf, 'w') as zip_file:
|
| 44 |
for i, sticker in enumerate(processed):
|
| 45 |
buf = io.BytesIO()
|
| 46 |
sticker.save(buf, format='PNG')
|
| 47 |
-
|
|
|
|
| 48 |
zip_buf.seek(0)
|
| 49 |
return zip_buf
|
| 50 |
|
| 51 |
# Gradio Interface
|
| 52 |
-
image_upload = gr.
|
| 53 |
output = gr.File(label="Download Sticker(s)")
|
| 54 |
|
| 55 |
interface = gr.Interface(
|
|
|
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
import zipfile
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
def remove_background(image):
|
| 10 |
"""Remove background using rembg"""
|
|
|
|
| 30 |
def process_images(image_files):
|
| 31 |
processed = []
|
| 32 |
for img_file in image_files:
|
| 33 |
+
img = Image.open(img_file.name).convert("RGBA") # Use .name for Gradio file paths
|
| 34 |
no_bg = remove_background(img)
|
| 35 |
sticker = add_outline(no_bg)
|
| 36 |
processed.append(sticker)
|
| 37 |
|
|
|
|
| 38 |
if len(processed) == 1:
|
| 39 |
+
# Return single PNG
|
| 40 |
buf = io.BytesIO()
|
| 41 |
processed[0].save(buf, format='PNG')
|
| 42 |
+
buf.seek(0)
|
| 43 |
+
return buf
|
| 44 |
else:
|
| 45 |
+
# Return ZIP
|
| 46 |
zip_buf = io.BytesIO()
|
| 47 |
with zipfile.ZipFile(zip_buf, 'w') as zip_file:
|
| 48 |
for i, sticker in enumerate(processed):
|
| 49 |
buf = io.BytesIO()
|
| 50 |
sticker.save(buf, format='PNG')
|
| 51 |
+
buf.seek(0)
|
| 52 |
+
zip_file.writestr(f'sticker_{i+1}.png', buf.read())
|
| 53 |
zip_buf.seek(0)
|
| 54 |
return zip_buf
|
| 55 |
|
| 56 |
# Gradio Interface
|
| 57 |
+
image_upload = gr.File(label="Upload Images (Single or Batch)", file_types=["image"], file_count="multiple")
|
| 58 |
output = gr.File(label="Download Sticker(s)")
|
| 59 |
|
| 60 |
interface = gr.Interface(
|