-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathremove_bg.py
51 lines (39 loc) · 1.21 KB
/
remove_bg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from tkinter import *
from rembg import remove
from PIL import Image, ImageTk
from tkinter import filedialog
root = Tk()
root.title("Remove Image Background")
root.geometry('500x700')
# Functions
def open_thing():
global input_path, my_img
# open image path
input_path = filedialog.askopenfilename(title="Open Image",
filetype=(("PNG Files", ".png"), ("All Files", "*.*")))
if input_path:
my_img = ImageTk.PhotoImage(Image.open(input_path))
pic_label.config(image=my_img, bg="black")
def remove_thing():
# get file path to save file
output_path = filedialog.asksaveasfilename(title="Save As",
filetype=(("PNG Files", '.png'), ("All Files", "*.*")))
# get file name
input = Image.open(input_path)
# remove bg
output = remove(input)
# Save the file
output.save(output_path, 'png')
# Put new image on the screen
global my_img
my_img = ImageTk.PhotoImage(Image.open(output_path))
# Update label
pic_label.config(image=my_img)
# Gui
pic_label = Label(root, text='')
pic_label.pack(pady=20)
open_button = Button(root, text="Open Image", command=open_thing)
open_button.pack(pady=20)
remove_button = Button(root, text="Remove Background", command=remove_thing)
remove_button.pack(pady=10)
root.mainloop()