Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,27 @@ from rembg import remove
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
def remove_bg(input_image: Image.Image) -> Image.Image:
|
| 6 |
-
"""Remove background from a PIL image and return the result."""
|
|
|
|
| 7 |
output = remove(input_image)
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
iface = gr.Interface(
|
| 11 |
fn=remove_bg,
|
| 12 |
inputs=gr.Image(type="pil", label="Upload any image"),
|
| 13 |
outputs=gr.Image(type="pil", label="Background removed"),
|
| 14 |
title="🖼️ Background Remover",
|
| 15 |
-
description="Upload an image and get it back with the background stripped away.",
|
| 16 |
examples=None,
|
| 17 |
)
|
| 18 |
-
|
| 19 |
if __name__ == "__main__":
|
| 20 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
def remove_bg(input_image: Image.Image) -> Image.Image:
|
| 6 |
+
"""Remove background from a PIL image and return the result with a white background."""
|
| 7 |
+
# remove original background (transparent)
|
| 8 |
output = remove(input_image)
|
| 9 |
+
# ensure output has alpha channel
|
| 10 |
+
if output.mode != "RGBA":
|
| 11 |
+
output = output.convert("RGBA")
|
| 12 |
+
# create white background image
|
| 13 |
+
white_bg = Image.new("RGBA", output.size, (255, 255, 255, 255))
|
| 14 |
+
# composite the output onto white background
|
| 15 |
+
white_bg.paste(output, (0, 0), output)
|
| 16 |
+
# convert to RGB, discarding alpha channel
|
| 17 |
+
return white_bg.convert("RGB")
|
| 18 |
|
| 19 |
iface = gr.Interface(
|
| 20 |
fn=remove_bg,
|
| 21 |
inputs=gr.Image(type="pil", label="Upload any image"),
|
| 22 |
outputs=gr.Image(type="pil", label="Background removed"),
|
| 23 |
title="🖼️ Background Remover",
|
| 24 |
+
description="Upload an image and get it back with the background stripped away and replaced with a white canvas.",
|
| 25 |
examples=None,
|
| 26 |
)
|
| 27 |
+
|
| 28 |
if __name__ == "__main__":
|
| 29 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|