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 |
+
def remove_background(input_image):
|
7 |
+
# Convert the image to bytes
|
8 |
+
img_byte_arr = io.BytesIO()
|
9 |
+
input_image.save(img_byte_arr, format="PNG")
|
10 |
+
img_byte_arr = img_byte_arr.getvalue()
|
11 |
+
|
12 |
+
# Apply background removal using rembg
|
13 |
+
output_bytes = remove(img_byte_arr)
|
14 |
+
|
15 |
+
# Convert the output (bytes) back into a PIL image
|
16 |
+
output_image = Image.open(io.BytesIO(output_bytes))
|
17 |
+
return output_image
|
18 |
+
|
19 |
+
# Create the Gradio interface
|
20 |
+
with gr.Blocks() as demo:
|
21 |
+
gr.Markdown("# Background Removal Tool")
|
22 |
+
|
23 |
+
with gr.Row():
|
24 |
+
input_image = gr.Image(type="pil", label="Upload an image")
|
25 |
+
output_image = gr.Image(label="Processed Image")
|
26 |
+
|
27 |
+
submit_button = gr.Button("Submit")
|
28 |
+
submit_button.click(remove_background, inputs=input_image, outputs=output_image)
|
29 |
+
|
30 |
+
# Optional: Add a clear button
|
31 |
+
clear_button = gr.Button("Clear")
|
32 |
+
def clear_images():
|
33 |
+
return None
|
34 |
+
clear_button.click(clear_images, inputs=None, outputs=input_image)
|
35 |
+
|
36 |
+
# Launch the Gradio app
|
37 |
+
demo.launch()
|