import gradio as gr from rembg import remove from PIL import Image def remove_bg(input_image: Image.Image) -> Image.Image: """Remove background from a PIL image and return the result with a white background.""" # remove original background (transparent) output = remove(input_image) # ensure output has alpha channel if output.mode != "RGBA": output = output.convert("RGBA") # create white background image white_bg = Image.new("RGBA", output.size, (255, 255, 255, 255)) # composite the output onto white background white_bg.paste(output, (0, 0), output) # convert to RGB, discarding alpha channel return white_bg.convert("RGB") iface = gr.Interface( fn=remove_bg, inputs=gr.Image(type="pil", label="Upload any image"), outputs=gr.Image(type="pil", label="Background removed"), title="🖼️ Background Remover", description="Upload an image and get it back with the background stripped away and replaced with a white canvas.", examples=None, ) if __name__ == "__main__": iface.launch(server_name="0.0.0.0", server_port=7860)