In this video I’ll show you how build an image background remover app with Tkinter and Python.

We’ll use the rembg library to easily remove the background from images.

Then we’ll save the image file to our computer using a tkinter filedialog.

Python Code: remove_bg.py
(Github Code)

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()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...