rishi9440 commited on
Commit
a4b35a8
·
1 Parent(s): a135247

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -1,22 +1,31 @@
1
  import gradio as gr
2
  from rembg import remove
3
  from PIL import Image
 
4
 
5
  def remove_background(input_image):
6
-
7
  input = Image.open(input_image) # load image
8
  output = remove(input) # remove background
9
  return output
10
 
 
 
 
 
 
 
 
 
 
11
 
12
- input_image = gr.inputs.Image(type="filepath", label="Input Image")
13
- output_image = gr.inputs.Image(type="filepath", label="Output Image")
14
- # Create a Gradio interface
15
- iface = gr.Interface(remove_background,
16
- inputs=input_image,
17
- outputs=output_image,
18
- title='Image Background Remover',
19
- description='Upload an image and it will remove.')
20
 
21
- # Launch the interface
 
22
  iface.launch()
 
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
  input = Image.open(input_image) # load image
8
  output = remove(input) # remove background
9
  return output
10
 
11
+ def download_output(output_image):
12
+ output_bytes = io.BytesIO()
13
+ output_image.save(output_bytes, format="PNG")
14
+ output_bytes.seek(0)
15
+ return output_bytes
16
+
17
+ input_image = gr.inputs.Image(type="file", label="Input Image")
18
+ output_image = gr.outputs.Image(type="pil", label="Output Image")
19
+ output_image_download = gr.outputs.Button(label="Download Output")
20
 
21
+ def background_removal_with_download(input_image):
22
+ output_image = remove_background(input_image)
23
+ output_image_download = gr.outputs.Button(label="Download Output")
24
+ download_button = gr.Interface(
25
+ download_output, inputs=gr.inputs.Image(type="pil", label="Output Image"), outputs="bytes",
26
+ interpretation="download", title="Download Output Image", live=False)
27
+ return output_image, download_button(output_image)
 
28
 
29
+ iface = gr.Interface(background_removal_with_download, inputs=input_image, outputs=[output_image, output_image_download],
30
+ title='Image Background Remover', description='Upload an image and it will remove.')
31
  iface.launch()