Download app.py from Shashi23/bgremove: direct link, hf CLI and curl.
- Browser
- Download file 861 Bytes
-
https://huggingface.co/spaces/Shashi23/bgremove/resolve/main/app.py
- Command line
-
hf download hf://spaces/Shashi23/bgremove/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Shashi23/bgremove/resolve/main/app.py
861 Bytes
| import gradio as gr | |
| import numpy as np | |
| from rembg import remove | |
| from PIL import Image | |
| def remove_bg_and_replace(image, background=None): | |
| if image is None: | |
| return None | |
| # Convert to RGBA and remove background | |
| image = image.convert("RGBA") | |
| removed_bg = remove(image) | |
| if background is not None: | |
| background = background.convert("RGBA").resize(image.size) | |
| combined = Image.alpha_composite(background, removed_bg) | |
| return combined | |
| return removed_bg | |
| description = "Upload an image to remove the background and optionally add a new one." | |
| demo = gr.Interface( | |
| fn=remove_bg_and_replace, | |
| inputs=[gr.Image(type="pil"), gr.Image(type="pil")], # Removed optional=True | |
| outputs=gr.Image(type="pil"), | |
| title="Background Removal & Replacement", | |
| description=description, | |
| ) | |
| demo.launch() | |