|
|
import gradio as gr |
|
|
from rembg import remove |
|
|
from PIL import Image |
|
|
import io |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_bg(image: Image.Image): |
|
|
if image is None: |
|
|
return None |
|
|
|
|
|
|
|
|
img_byte_arr = io.BytesIO() |
|
|
image.save(img_byte_arr, format='PNG') |
|
|
img_bytes = img_byte_arr.getvalue() |
|
|
|
|
|
|
|
|
output_bytes = remove(img_bytes) |
|
|
|
|
|
|
|
|
output_image = Image.open(io.BytesIO(output_bytes)) |
|
|
|
|
|
return output_image |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|