Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,42 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from rembg import remove
|
3 |
from PIL import Image
|
4 |
-
import
|
5 |
|
6 |
def remove_background(input_image):
|
7 |
input = Image.open(input_image) # load image
|
8 |
output = remove(input) # remove background
|
9 |
return output
|
10 |
|
|
|
|
|
11 |
input_image = gr.inputs.Image(type="filepath", label="Input Image")
|
12 |
output_image = gr.outputs.Image(type="filepath", label="Output Image")
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
iface.launch()
|
30 |
|
31 |
-
output_pil = iface.get_output() # get the output PIL image from the interface
|
32 |
-
output_base64_str = pil_to_base64_string(output_pil) # convert the PIL image to base64 string
|
33 |
-
with open("output_image.png", "wb") as f:
|
34 |
-
f.write(base64.b64decode(output_base64_str)) # write the decoded base64 string to a file
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
import gradio as gr
|
6 |
from rembg import remove
|
7 |
from PIL import Image
|
8 |
+
from io import BytesIO
|
9 |
|
10 |
def remove_background(input_image):
|
11 |
input = Image.open(input_image) # load image
|
12 |
output = remove(input) # remove background
|
13 |
return output
|
14 |
|
15 |
+
# define input and output interfaces
|
16 |
+
|
17 |
input_image = gr.inputs.Image(type="filepath", label="Input Image")
|
18 |
output_image = gr.outputs.Image(type="filepath", label="Output Image")
|
19 |
|
20 |
+
# define function for downloading output image
|
21 |
+
def download_output(output_image):
|
22 |
+
output_image.save("output.png")
|
23 |
+
with open("output.png", "rb") as f:
|
24 |
+
img_bytes = BytesIO(f.read())
|
25 |
+
return img_bytes
|
26 |
+
|
27 |
+
# define output download button
|
28 |
+
output_image_download = gr.outputs.Button(label="Download Output", type="bytes", callback=download_output)
|
29 |
+
|
30 |
+
# create the interface
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=remove_background,
|
33 |
+
inputs=input_image,
|
34 |
+
outputs=output_image,
|
35 |
+
output_options=[output_image_download],
|
36 |
+
title='Image Background Remover',
|
37 |
+
description='Upload an image and it will remove.'
|
38 |
+
)
|
39 |
+
|
40 |
+
# launch the interface
|
41 |
iface.launch()
|
42 |
|
|
|
|
|
|
|
|