rishi9440 commited on
Commit
f036466
·
1 Parent(s): 80f97ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
app.py CHANGED
@@ -1,34 +1,42 @@
 
 
 
 
1
  import gradio as gr
2
  from rembg import remove
3
  from PIL import Image
4
- import base64
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
- # 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
- # Define a function to convert PIL image to base64 string
22
- def pil_to_base64_string(pil_image):
23
- buffered = BytesIO()
24
- pil_image.save(buffered, format="PNG")
25
- img_str = base64.b64encode(buffered.getvalue()).decode()
26
- return img_str
27
-
28
- # Launch the interface
 
 
 
 
 
 
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