File size: 1,110 Bytes
dfb188e
 
 
 
 
ea7d8f6
 
dfb188e
ea7d8f6
 
 
 
 
 
 
 
 
dfb188e
 
 
 
 
 
ea7d8f6
dfb188e
 
ea7d8f6
dfb188e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)