File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ from rembg import remove
2
+ from PIL import Image
3
+
4
+
5
+ def remove_bg (input_path , output_path ):
6
+ """Removes the background from an image and saves the result."""
7
+
8
+ try :
9
+ input_img = Image .open (input_path )
10
+ output_img = remove (input_img )
11
+ output_img .save (output_path )
12
+ print (f"Background removed successfully! Saved to { output_path } " )
13
+ except FileNotFoundError :
14
+ print (f"Error: File not found at { input_path } " )
15
+ except Exception as e :
16
+ print (f"An error occurred: { e } " )
17
+
18
+
19
+ def main ():
20
+ input_path = input ("Enter the path to your image: " )
21
+ output_choice = input ("Do you want to specify the output path? (y/n): " )
22
+
23
+ if output_choice .lower () == 'y' :
24
+ output_path = input ("Enter the desired output path: " )
25
+ else :
26
+ output_path = "removed_bg.png" # Default output name
27
+
28
+ remove_bg (input_path , output_path )
29
+
30
+
31
+ if __name__ == "__main__" :
32
+ main ()
You can’t perform that action at this time.
0 commit comments