Jeffgold commited on
Commit
09e782c
·
1 Parent(s): 1b1a07b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -53
app.py CHANGED
@@ -1,55 +1,20 @@
1
- import os
2
- import sys
3
- import logging
4
  from rembg import remove
5
- from PIL import Image
6
 
7
- # Set up logging
8
- logging.basicConfig(level=logging.INFO, format='%(message)s')
9
-
10
- def process_folder(input_folder, output_folder):
11
- # Ensure the output folder exists
12
- os.makedirs(output_folder, exist_ok=True)
13
-
14
- # Define supported image extensions
15
- supported_extensions = ('.png', '.jpg', '.jpeg')
16
-
17
- # Iterate over all files in the input folder
18
- for filename in os.listdir(input_folder):
19
- # Check if the file is an image
20
- if filename.lower().endswith(supported_extensions):
21
- try:
22
- # Load the image
23
- image_path = os.path.join(input_folder, filename)
24
- image = Image.open(image_path)
25
-
26
- # Remove the background
27
- image_no_bg = remove(image)
28
-
29
- # Save the output image with the prefix 'bg-rm_'
30
- output_filename = 'bg-rm_' + filename
31
- output_path = os.path.join(output_folder, output_filename)
32
- image_no_bg.save(output_path)
33
-
34
- # Log success
35
- logging.info(f'Processed: {filename}')
36
-
37
- except Exception as e:
38
- # Log error
39
- logging.error(f'Error processing {filename}: {e}')
40
-
41
- def main():
42
- # Check command-line arguments
43
- if len(sys.argv) != 3:
44
- print(f'Usage: {sys.argv[0]} input_folder output_folder')
45
- sys.exit(1)
46
-
47
- # Get input and output folders from command-line arguments
48
- input_folder = sys.argv[1]
49
- output_folder = sys.argv[2]
50
-
51
- # Process the images in the input folder
52
- process_folder(input_folder, output_folder)
53
-
54
- if __name__ == '__main__':
55
- main()
 
1
+ import gradio as gr
 
 
2
  from rembg import remove
 
3
 
4
+ title = "Vanish Background"
5
+ description = "Remove background for any image. To use it, simply upload your image(s) and wait. Read more at the link below of official documentation."
6
+ article = "<p style='text-align: center;'><a href='https://github.com/danielgatis/rembg' target='_blank'>Github Repo</a></p>"
7
+
8
+ def segment(images):
9
+ # Process each image in the list and remove the background
10
+ return [remove(image) for image in images]
11
+
12
+ demo = gr.Interface(
13
+ fn=segment,
14
+ inputs=gr.inputs.Image(type="file", label="Upload Image(s)", multiple=True),
15
+ outputs=gr.outputs.Image(type="file", label="Image(s) with Background Removed"),
16
+ title=title,
17
+ description=description,
18
+ article=article
19
+ )
20
+ demo.launch()