|
import gradio as gr |
|
from rembg import remove |
|
from PIL import Image |
|
import io |
|
|
|
def background_remover(input_image): |
|
input_bytes = input_image.read() |
|
input_img = Image.open(io.BytesIO(input_bytes)) |
|
output_img = remove(input_img) |
|
|
|
buf = io.BytesIO() |
|
output_img.save(buf, format='PNG') |
|
buf.seek(0) |
|
return buf |
|
|
|
description = """ |
|
This app uses the rembg library to remove the background from uploaded images. |
|
Simply upload your image and let the model do its work. Download the result immediately after! |
|
""" |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("<h1 style='text-align: center;'>Background Remover</h1>") |
|
gr.Markdown(description) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
input_image = gr.Image(type='pil', label="Upload Image") |
|
with gr.Column(scale=1): |
|
output_image = gr.Image(type='pil', label="Result") |
|
|
|
with gr.Row(): |
|
gr.Button("Remove Background").click( |
|
fn=background_remover, |
|
inputs=input_image, |
|
outputs=output_image |
|
) |
|
|
|
demo_examples = [["woman.jpg"], ["groot.jpg"]] |
|
demo.launch(share=True, examples=demo_examples, theme="soft") |
|
|