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()