Spaces:
Sleeping

bg_api / app.py
sk5252's picture
Update app.py
8cdba64 verified
Raw History Blame Contribute Delete
1.04 kB
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