Removing image backgrounds with python and ai

Removing backgrounds is a common task most people have dealt with it in some way or another: cleaning photos for marketing, removing unaesthetic backdrops from selfies or swapping creating photo collages are just a few examples involving it. But what was once a task reserved for people skilled with image editing software has become an easy feat for people that can run python code, thanks to developments in ai.

Meet rembg

The rembg project is an MIT-licensed open source project that specializes in removing backgrounds from images. It is available as a python library for fully automated processing, or as a cli/web tool for more fine-tuned on-demand background removal. While it supports GPU hardware to speed up processing times, it will run just fine on CPU alone if the host does not have access to supported hardware.

Installation can be done through pip, with the command slightly differing depending on which version is desired:

pip install rembg # use as python library 
pip install rembg[cli] # use from command-line or web ui

Using the standalone version

The simplest way to use the cli version is to make it serve a web ui through the builtin webserver:

rembg s

This will start a gradio web interface at localhost:5000, which should open automatically in your preferred browser:

For testing purposes, we will use this selfie photo from pexels. Running it through the removal tool yields some very impressive results in just about one second:

Even though our test went well, your experience may differ. If you are not satisfied with the results, try one of the other models that are included in rembg. There are some more specialized for animes or humans, and several general-purpose variants available out of the box.

Removing backgrounds with python

Since the tool is written in python, it can be easily be imported and used from python code directly:

import rembg

with open("in.png", 'rb') as input_file, open("out.png", 'wb') as output_file:

This would read the image in.png, remove it's background and save the result to out.png in the current directory. While this works the same as the web interface variant, the rembg.remove() function has several optional arguments that provide more flexibility.

You could, for example, set the removed background to a solid color with the bgcolor parameter:

import rembg

with open("in.png", 'rb') as input_file, open("out.png", 'wb') as output_file:
    output_file.write(rembg.remove(, bgcolor=(22, 160, 133, 255)))

The parameter takes a tuple with exactly 4 values, representing an RGBA color. In our example, the values (22, 160, 133, 255) correspond to a friendly green, so the output image now has that background color:

And that's really all there is to the modern approach to image background removal. Do give it a try yourself, it is a very fast and rewarding experience, finally enabling developers and those able to run python code to automate one more boring task.

More articles

Building a text editor in Python with Tkinter

Creating desktop software without dependencies

Managing virtual environments in python

Managing dependencies in python projects

Generator functions in python

Generating sequences on the fly, one by one

Writing kubernetes manifests by hand

Writing valid and secure object definitions

PHP output buffering

Controlling what gets sent and when

Automating local domains for minikube

Local ingress domains that just work