Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
-
import zipfile
|
| 8 |
|
| 9 |
def remove_background(image):
|
| 10 |
-
|
| 11 |
img_byte_arr = io.BytesIO()
|
| 12 |
image.save(img_byte_arr, format='PNG')
|
| 13 |
-
result_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
|
| 17 |
-
"""Add white outline to transparent image"""
|
| 18 |
-
alpha = image.split()[-1]
|
| 19 |
-
dilated_alpha = alpha.filter(ImageFilter.MaxFilter(outline_width * 2 + 1))
|
| 20 |
-
outline_mask = ImageChops.difference(dilated_alpha, alpha)
|
| 21 |
-
|
| 22 |
-
# Create outline layer
|
| 23 |
-
outline = Image.new('RGBA', image.size, (*outline_color, 255))
|
| 24 |
-
outline.putalpha(outline_mask)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
final = Image.alpha_composite(outline, image)
|
| 28 |
-
return final
|
| 29 |
-
|
| 30 |
-
def process_images(image_files):
|
| 31 |
-
# Create temporary directory
|
| 32 |
temp_dir = tempfile.mkdtemp()
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
img = Image.open(img_file.name).convert("RGBA")
|
| 38 |
-
no_bg = remove_background(img)
|
| 39 |
-
sticker = add_outline(no_bg)
|
| 40 |
-
|
| 41 |
-
# Save to temporary file
|
| 42 |
-
file_path = os.path.join(temp_dir, f"sticker_{i+1}.png")
|
| 43 |
-
sticker.save(file_path)
|
| 44 |
-
processed_paths.append(file_path)
|
| 45 |
-
|
| 46 |
-
# Return single file or ZIP
|
| 47 |
-
if len(processed_paths) == 1:
|
| 48 |
-
return processed_paths[0]
|
| 49 |
-
else:
|
| 50 |
-
zip_path = os.path.join(temp_dir, "stickers.zip")
|
| 51 |
-
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 52 |
-
for p in processed_paths:
|
| 53 |
-
zipf.write(p, os.path.basename(p))
|
| 54 |
-
return zip_path
|
| 55 |
-
except Exception as e:
|
| 56 |
-
# Cleanup on error
|
| 57 |
-
for p in processed_paths:
|
| 58 |
-
try:
|
| 59 |
-
os.remove(p)
|
| 60 |
-
except: pass
|
| 61 |
-
raise e
|
| 62 |
|
| 63 |
# Gradio Interface
|
| 64 |
-
image_upload = gr.
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
output = gr.File(label="Download Sticker(s)")
|
| 69 |
|
| 70 |
interface = gr.Interface(
|
| 71 |
-
fn=
|
| 72 |
inputs=image_upload,
|
| 73 |
-
outputs=
|
| 74 |
-
title="
|
| 75 |
-
description="Remove background
|
| 76 |
-
allow_flagging="never"
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
import tempfile
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
def remove_background(image):
|
| 9 |
+
# Process image
|
| 10 |
img_byte_arr = io.BytesIO()
|
| 11 |
image.save(img_byte_arr, format='PNG')
|
| 12 |
+
result_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 13 |
+
processed_img = Image.open(io.BytesIO(result_bytes)).convert("RGBA")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Save to temporary file for download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
temp_dir = tempfile.mkdtemp()
|
| 17 |
+
file_path = os.path.join(temp_dir, "no_background.png")
|
| 18 |
+
processed_img.save(file_path)
|
| 19 |
+
|
| 20 |
+
return processed_img, file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Gradio Interface
|
| 23 |
+
image_upload = gr.Image(label="Upload Image", type="pil")
|
| 24 |
+
output_preview = gr.Image(label="Preview", type="pil")
|
| 25 |
+
output_file = gr.File(label="Download PNG")
|
|
|
|
|
|
|
| 26 |
|
| 27 |
interface = gr.Interface(
|
| 28 |
+
fn=remove_background,
|
| 29 |
inputs=image_upload,
|
| 30 |
+
outputs=[output_preview, output_file],
|
| 31 |
+
title=" PNG Background Remover",
|
| 32 |
+
description="Remove background and preview/download as transparent PNG",
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|