Spaces:
Runtime error
Runtime error
File size: 2,412 Bytes
8899aca b22cbe3 8899aca b22cbe3 8899aca 7270461 b22cbe3 8899aca b22cbe3 7270461 b22cbe3 7270461 b22cbe3 c085c3d 8899aca d3a9dfd 41eb08d 8899aca b22cbe3 41eb08d 3444052 c085c3d 3444052 8899aca c085c3d 8899aca b22cbe3 7270461 8899aca 7270461 8899aca 7270461 c085c3d 7270461 c085c3d 7270461 8899aca | 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import gradio as gr
from rembg import remove
from PIL import Image
import io
import os
OUTPUT_FILE = "output.png"
# Background removal function
def remove_background(image):
input_bytes = io.BytesIO()
image.save(input_bytes, format='PNG')
input_bytes = input_bytes.getvalue()
output_bytes = remove(input_bytes)
output_img = Image.open(io.BytesIO(output_bytes))
output_img.save(OUTPUT_FILE, format="PNG")
return OUTPUT_FILE, "Background removed successfully!"
# Example images
example_images = [
"examples/1.jpg",
"examples/2.jpg",
"examples/3.jpg",
"examples/4.jpg",
"examples/5.jpg",
"examples/6.png"
]
with gr.Blocks(title="Background Remover") as demo:
gr.Markdown("# Background Remover")
gr.Markdown("""
Upload an image or use an example to remove its background.
PS: Not the best image background remover out there but good enough for a free tool.
""")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Input Image")
submit_btn = gr.Button("Submit")
clear_btn = gr.Button("Clear")
with gr.Column():
output_image = gr.Image(label="Output", type="filepath")
output_text = gr.Textbox(label="Status", lines=1)
download_html = gr.HTML(visible=False)
with gr.Row():
gr.Markdown("### Example Images")
with gr.Row():
gr.Examples(
examples=example_images,
inputs=image_input,
label="Click to try an example",
)
# Logic
def process(img):
output_path, msg = remove_background(img)
download_button = f"""
<a href="file/{output_path}" download style="
display:inline-block;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
text-align: center;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
margin-top: 10px;
">⬇ Download Output</a>
"""
return output_path, msg, gr.update(visible=True, value=download_button)
submit_btn.click(fn=process, inputs=image_input, outputs=[output_image, output_text, download_html])
clear_btn.click(fn=lambda: (None, "", gr.update(visible=False, value="")), outputs=[image_input, output_text, download_html])
demo.launch()
|