cr8 commited on
Commit
532cd7b
·
verified ·
1 Parent(s): fc53748

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -2,22 +2,21 @@ import gradio as gr
2
  from PIL import Image
3
  import rembg
4
  import io
5
- import tempfile
6
- import os
7
 
8
  def remove_background(image):
9
  # Process image
10
  img_byte_arr = io.BytesIO()
11
  image.save(img_byte_arr, format='PNG')
12
- result_bytes = rembg.remove(img_byte_arr.getvalue())
13
- processed_img = Image.open(io.BytesIO(result_bytes)).convert("RGBA")
14
 
15
- # Save to temporary file for download
16
- temp_dir = tempfile.mkdtemp()
17
- file_path = os.path.join(temp_dir, "no_background.png")
18
- processed_img.save(file_path)
19
 
20
- return processed_img, file_path
 
 
 
 
21
 
22
  # Gradio Interface
23
  image_upload = gr.Image(label="Upload Image", type="pil")
@@ -30,6 +29,7 @@ interface = gr.Interface(
30
  outputs=[output_preview, output_file],
31
  title=" PNG Background Remover",
32
  description="Remove background and preview/download as transparent PNG",
 
33
  )
34
 
35
  if __name__ == "__main__":
 
2
  from PIL import Image
3
  import rembg
4
  import io
 
 
5
 
6
  def remove_background(image):
7
  # Process image
8
  img_byte_arr = io.BytesIO()
9
  image.save(img_byte_arr, format='PNG')
10
+ processed_bytes = rembg.remove(img_byte_arr.getvalue())
 
11
 
12
+ # Create PIL image for preview
13
+ processed_img = Image.open(io.BytesIO(processed_bytes))
 
 
14
 
15
+ # Prepare download file
16
+ download_bytes = io.BytesIO(processed_bytes)
17
+ download_bytes.seek(0) # Reset buffer position
18
+
19
+ return processed_img, download_bytes
20
 
21
  # Gradio Interface
22
  image_upload = gr.Image(label="Upload Image", type="pil")
 
29
  outputs=[output_preview, output_file],
30
  title=" PNG Background Remover",
31
  description="Remove background and preview/download as transparent PNG",
32
+ allow_flagging="never"
33
  )
34
 
35
  if __name__ == "__main__":