Easily remove background from images with Python

With only 5 lines of code!


Easily remove background from images with Python

I would like to make a very detailed text, but this tool in Python is very simple and straight to the point!

Just install pip and with pip install the rembg package:

pip install rembg --user

The --user parameter will make this package/module/library available only to your user: ${HOME} you must have the directory ~/.local/bin included in your environment variable ${PATH }, if not, run the command:

echo 'export PATH="${PATH}:${HOME}/.local/bin"' >> ~/.bashrc

If you want it for all system users, run: sudo pip install rembg.

Now just create some basic code, example:

code.py

from rembg import remove
from PIL import Image
input = Image.open("bird.png")
output = remove(input)
output.save("output.png")

Run: python code.py, the backgroundless image will be saved in the directory where you executed the code and its name will be: output.png

If you want to test with the image of this example, save it:

bird.png


python


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles