Spaces:
Sleeping
Sleeping
File size: 753 Bytes
e41c2b6 2991511 69489a7 5d525d7 69489a7 5d525d7 69489a7 5d525d7 69489a7 2991511 69489a7 e41c2b6 | 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 | import gradio as gr
from rembg import remove
from PIL import Image
import io
def remove_bg(input_image):
try:
if input_image is None:
return None
# Convert input image to bytes
input_bytes = io.BytesIO()
input_image.save(input_bytes, format='PNG')
input_bytes = input_bytes.getvalue()
# Run background removal
output_bytes = remove(input_bytes)
# Return RGBA image (with transparent background)
output_image = Image.open(io.BytesIO(output_bytes)).convert("RGBA")
return output_image
except Exception as e:
print("Error:", e)
return None
demo = gr.Interface(fn=remove_bg, inputs=gr.Image(type="pil"), outputs="image")
demo.launch()
|