Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from rembg import remove
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Function to remove the background
|
| 7 |
+
def remove_background(image):
|
| 8 |
+
# Convert the uploaded image to bytes
|
| 9 |
+
img_bytes = io.BytesIO()
|
| 10 |
+
image.save(img_bytes, format="PNG")
|
| 11 |
+
img_bytes = img_bytes.getvalue()
|
| 12 |
+
|
| 13 |
+
# Use rembg to remove the background
|
| 14 |
+
output_bytes = remove(img_bytes)
|
| 15 |
+
|
| 16 |
+
# Convert the output bytes back to an image
|
| 17 |
+
output_image = Image.open(io.BytesIO(output_bytes)).convert("RGBA")
|
| 18 |
+
return output_image
|
| 19 |
+
|
| 20 |
+
# Gradio interface
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
gr.Markdown("# Background Remover AI")
|
| 23 |
+
gr.Markdown("Upload an image, and the AI will remove the background for you.")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
input_image = gr.Image(type="pil", label="Upload Image")
|
| 28 |
+
with gr.Column():
|
| 29 |
+
output_image = gr.Image(type="pil", label="Output Image")
|
| 30 |
+
|
| 31 |
+
remove_button = gr.Button("Remove Background")
|
| 32 |
+
|
| 33 |
+
# Link the button to the function
|
| 34 |
+
remove_button.click(remove_background, inputs=input_image, outputs=output_image)
|
| 35 |
+
|
| 36 |
+
# Launch the app
|
| 37 |
+
demo.launch()
|