[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Use rembg to remove the background from images in Python

[Two pictures with their backgrounds removed by rembg in Python]

The rembg library makes it fairly easy to remove the background from an image. This example uses the tool to remove the backgrounds from two image files and then displays the original and modified files.

Before you can use the library, however, you need to install it. Just use this command:

pip install rembg

If you want to install both the library and the command-line interface, use this command:

pip install "rembg[cli]"

Removing Backgrounds

The following remove_background function wraps up the process of removing the background from an image.

from rembg import remove from PIL import Image import os def remove_background(infile, outfile): curdir = os.getcwd() infile = os.path.join(curdir, infile) outfile = os.path.join(curdir, outfile) input_image = Image.open(infile) output_image = remove(input_image) output_image.save(outfile)

This function assumes you're trying to process a file in the same directory as the program. It uses os.getcwd to get the program's working directory and then adds that directory to the input and output files to get complete paths to the files.

Next, the code uses the open function in PIL's Image module to open the input file. It passes the returned Image object to rembg's remove method to remove the background image. Finally, it saves the modified image into the output file.

Tip: If you want to the output file to have a transparent background, save it as a PNG file.

Using remove_background

When the program starts, it creates a RembgApp object. That object's constructor, which is shown in the following code, sets up tkinter, calls remove_background to process two images, and displays the results.

class RembgApp: def __init__(self): self.window = tk.Tk() self.window.title('use_rembg') self.window.protocol('WM_DELETE_WINDOW', self.kill_callback) # Process the images. self.images = [] col = 0 files = [('cake1.png', 'cake1_tr.png'), ('cake2.jpg', 'cake2_tr.png')] for infile, outfile in files: print(f'{infile} --> {outfile}') remove_background(infile, outfile) image = ImageTk.PhotoImage(Image.open(infile)) self.images.append(image) label = tk.Label(self.window, image=image) label.grid(row=0, column=col, padx=1, pady=1) image = Image.open(outfile) image = draw_over_checkerboard(image) image = ImageTk.PhotoImage(image) self.images.append(image) label = tk.Label(self.window, image=image) label.grid(row=1, column=col, padx=(0,1), pady=(0,1)) col += 1 # Display the window. self.window.focus_force() self.window.mainloop()

This code prepares tkinter. It then loops through two sets of input and output file names. For each file, it calls remove_background to remove the background from the input file and save the result in the output file.

Next, the code opens the input file and converts it into a PhotoImage so tkinter can display it. It creates a Label widget and displays the input image in it.

The code repeats those steps to display the result image with its background removed. This time it calls the draw_over_checkerboard function to draw the resulting image file on top of a checkered background so you can see where the image is transparent. (For information on that function, see my post Use PIL to draw a transparent image over a checkerboard in Python.)

After it processes the files, the code runs the tkinter main loop so you can see the images.

Conclusion

Using rembg is pretty easy and the remove_background function makes it even easier.

Take a look at the picture at the top of this post. The library does a good job of removing the background from the image on the left. It does less well with the image on the right. If you look closely at the lower left image, you'll see some areas on the lower left, upper middle, and middle right of the cake where the checkerboard is shaded. It's possible that the remove function "thought" those areas were shadows that should be preserved.

In any case, rembg produces as pretty good result for so little effort.

For more details about rembg, see its github page https://github.com/danielgatis/rembg.

Download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.