Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import rembg
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
def remove_background(input_image):
|
8 |
+
# Convert Gradio input image data to bytes
|
9 |
+
image_array = input_image.astype(np.uint8) # Ensure the image is in the correct format
|
10 |
+
image_pil = Image.fromarray(image_array)
|
11 |
+
|
12 |
+
# Save the PIL Image to an in-memory buffer
|
13 |
+
image_buffer = io.BytesIO()
|
14 |
+
image_pil.save(image_buffer, format='PNG')
|
15 |
+
|
16 |
+
# Use rembg to remove the background
|
17 |
+
output_data = rembg.remove(image_buffer.getvalue())
|
18 |
+
|
19 |
+
# Convert the output data to a PIL Image
|
20 |
+
output_image = Image.open(io.BytesIO(output_data))
|
21 |
+
|
22 |
+
# Return the output image directly
|
23 |
+
return np.array(output_image)
|
24 |
+
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=remove_background,
|
27 |
+
inputs=gr.Image(),
|
28 |
+
outputs=gr.Image()
|
29 |
+
)
|
30 |
+
|
31 |
+
iface.launch()
|