Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
import zipfile
|
| 6 |
|
| 7 |
def remove_background(image):
|
| 8 |
-
"""Remove background
|
| 9 |
img_byte_arr = io.BytesIO()
|
| 10 |
image.save(img_byte_arr, format='PNG')
|
| 11 |
-
|
| 12 |
-
return Image.open(io.BytesIO(
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Gradio Interface
|
| 31 |
-
image_upload = gr.Files(label="Upload Images (Batch)", file_types=["image"])
|
| 32 |
-
|
| 33 |
|
| 34 |
interface = gr.Interface(
|
| 35 |
-
fn=
|
| 36 |
inputs=image_upload,
|
| 37 |
-
outputs=
|
| 38 |
-
title="
|
| 39 |
-
description="
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
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"""
|
| 9 |
img_byte_arr = io.BytesIO()
|
| 10 |
image.save(img_byte_arr, format='PNG')
|
| 11 |
+
result = rembg.remove(img_byte_arr.getvalue())
|
| 12 |
+
return Image.open(io.BytesIO(result)).convert("RGBA")
|
| 13 |
|
| 14 |
+
def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
|
| 15 |
+
"""Add white outline to transparent image"""
|
| 16 |
+
alpha = image.split()[-1]
|
| 17 |
+
dilated_alpha = alpha.filter(ImageFilter.MaxFilter(outline_width * 2 + 1))
|
| 18 |
+
outline_mask = ImageChops.difference(dilated_alpha, alpha)
|
| 19 |
+
|
| 20 |
+
# Create outline layer
|
| 21 |
+
outline = Image.new('RGBA', image.size, (*outline_color, 255))
|
| 22 |
+
outline.putalpha(outline_mask)
|
| 23 |
+
|
| 24 |
+
# Combine layers
|
| 25 |
+
final = Image.alpha_composite(outline, image)
|
| 26 |
+
return final
|
| 27 |
+
|
| 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 |
+
return buf.getvalue()
|
| 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 |
+
zip_file.writestr(f'sticker_{i+1}.png', buf.getvalue())
|
| 48 |
+
zip_buf.seek(0)
|
| 49 |
+
return zip_buf
|
| 50 |
|
| 51 |
# Gradio Interface
|
| 52 |
+
image_upload = gr.Files(label="Upload Images (Single or Batch)", file_types=["image"])
|
| 53 |
+
output = gr.File(label="Download Sticker(s)")
|
| 54 |
|
| 55 |
interface = gr.Interface(
|
| 56 |
+
fn=process_images,
|
| 57 |
inputs=image_upload,
|
| 58 |
+
outputs=output,
|
| 59 |
+
title="✨ Sticker Maker",
|
| 60 |
+
description="Remove background, add white outline, and download as PNG(s)",
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|