File size: 1,132 Bytes
85be54c
0118b63
85be54c
0118b63
85be54c
 
 
 
 
04df704
 
 
0118b63
 
 
 
85be54c
0118b63
 
85be54c
0118b63
 
85be54c
0118b63
85be54c
 
 
 
 
 
04df704
85be54c
04df704
 
 
85be54c
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from rembg import remove
from PIL import Image
import io

# -------------------------------
# Function to remove background
# -------------------------------
def remove_bg(image: Image.Image):
    if image is None:
        return None

    # Convert PIL image to bytes
    img_byte_arr = io.BytesIO()
    image.save(img_byte_arr, format='PNG')
    img_bytes = img_byte_arr.getvalue()

    # Remove background using rembg
    output_bytes = remove(img_bytes)

    # Convert bytes back to PIL image
    output_image = Image.open(io.BytesIO(output_bytes))

    return output_image

# -------------------------------
# Gradio UI
# -------------------------------
demo = gr.Interface(
    fn=remove_bg,
    inputs=gr.Image(type="pil", label="Upload any image"),
    outputs=gr.Image(type="pil", label="Background Removed"),
    title="Universal Background Remover",
    description="Upload any image and the background will be automatically removed using rembg.",
    allow_flagging="never",
)

# -------------------------------
# Launch
# -------------------------------
if __name__ == "__main__":
    demo.launch()