Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image, ImageChops, ImageFilter
|
| 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"""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return Image.open(
|
| 15 |
|
| 16 |
def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
|
| 17 |
"""Add white outline to transparent image"""
|
|
@@ -28,33 +27,43 @@ def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
|
|
| 28 |
return final
|
| 29 |
|
| 30 |
def process_images(image_files):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Gradio Interface
|
| 57 |
-
image_upload = gr.File(label="Upload Images (Single or Batch)",
|
|
|
|
|
|
|
|
|
|
| 58 |
output = gr.File(label="Download Sticker(s)")
|
| 59 |
|
| 60 |
interface = gr.Interface(
|
|
@@ -63,6 +72,7 @@ interface = gr.Interface(
|
|
| 63 |
outputs=output,
|
| 64 |
title="✨ Sticker Maker",
|
| 65 |
description="Remove background, add white outline, and download as PNG(s)",
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image, ImageChops, ImageFilter
|
| 3 |
import rembg
|
|
|
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
+
import zipfile
|
| 7 |
|
| 8 |
def remove_background(image):
|
| 9 |
"""Remove background using rembg"""
|
| 10 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp:
|
| 11 |
+
image.save(temp.name)
|
| 12 |
+
result = rembg.remove(temp.name)
|
| 13 |
+
return Image.open(result).convert("RGBA")
|
| 14 |
|
| 15 |
def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
|
| 16 |
"""Add white outline to transparent image"""
|
|
|
|
| 27 |
return final
|
| 28 |
|
| 29 |
def process_images(image_files):
|
| 30 |
+
# Create temporary directory
|
| 31 |
+
temp_dir = tempfile.mkdtemp()
|
| 32 |
+
try:
|
| 33 |
+
processed_paths = []
|
| 34 |
+
for i, img_file in enumerate(image_files):
|
| 35 |
+
# Process image
|
| 36 |
+
img = Image.open(img_file.name).convert("RGBA")
|
| 37 |
+
no_bg = remove_background(img)
|
| 38 |
+
sticker = add_outline(no_bg)
|
| 39 |
+
|
| 40 |
+
# Save to temporary file
|
| 41 |
+
file_path = os.path.join(temp_dir, f"sticker_{i+1}.png")
|
| 42 |
+
sticker.save(file_path)
|
| 43 |
+
processed_paths.append(file_path)
|
| 44 |
+
|
| 45 |
+
# Return single file or ZIP
|
| 46 |
+
if len(processed_paths) == 1:
|
| 47 |
+
return processed_paths[0]
|
| 48 |
+
else:
|
| 49 |
+
zip_path = os.path.join(temp_dir, "stickers.zip")
|
| 50 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 51 |
+
for p in processed_paths:
|
| 52 |
+
zipf.write(p, os.path.basename(p))
|
| 53 |
+
return zip_path
|
| 54 |
+
except Exception as e:
|
| 55 |
+
# Cleanup on error
|
| 56 |
+
for p in processed_paths:
|
| 57 |
+
try:
|
| 58 |
+
os.remove(p)
|
| 59 |
+
except: pass
|
| 60 |
+
raise e
|
| 61 |
|
| 62 |
# Gradio Interface
|
| 63 |
+
image_upload = gr.File(label="Upload Images (Single or Batch)",
|
| 64 |
+
file_types=["image"],
|
| 65 |
+
file_count="multiple")
|
| 66 |
+
|
| 67 |
output = gr.File(label="Download Sticker(s)")
|
| 68 |
|
| 69 |
interface = gr.Interface(
|
|
|
|
| 72 |
outputs=output,
|
| 73 |
title="✨ Sticker Maker",
|
| 74 |
description="Remove background, add white outline, and download as PNG(s)",
|
| 75 |
+
allow_flagging="never"
|
| 76 |
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|