Spaces:
Sleeping

File size: 1,044 Bytes
a4045f6
 
 
87e4304
a4045f6
8cdba64
 
 
 
 
 
87e4304
8cdba64
 
87e4304
8cdba64
 
 
 
 
 
 
a4045f6
87e4304
 
 
 
 
8cdba64
 
87e4304
 
8cdba64
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
import gradio as gr
from rembg import remove
from PIL import Image
import io

def remove_background(image: Image.Image):
    try:
        # Convert PIL to bytes
        byte_arr = io.BytesIO()
        image.save(byte_arr, format="PNG")
        input_bytes = byte_arr.getvalue()

        # Run background remover
        output_bytes = remove(input_bytes)

        # Convert back to PIL
        output = Image.open(io.BytesIO(output_bytes)).convert("RGBA")
        return output
    except Exception as e:
        # If something goes wrong, return a blank image + print error
        print("Error:", e)
        return Image.new("RGBA", (512, 512), (255, 0, 0, 0))  # transparent red

iface = gr.Interface(
    fn=remove_background,
    inputs=gr.Image(type="pil", label="Upload Image"),
    outputs=gr.Image(type="pil", label="Background Removed"),
    title="Background Remover",
    description="Upload an image to remove its background using rembg",
    api_name="predict"
)

iface.launch(show_error=True)   # 👈 IMPORTANT: show_error=True