How to Remove Image Background in Python

Introduction

A visual representation of something is known as an image. It can be two-dimensional or three-dimensional or feed into the optical system to convey information.

Images are generally composed of different elements, things surrounding nature. Sometimes we just want to remove that background so that we can edit and use it according to our needs.

For example, if you just clicked one candid picture working on the computer, and you liked that picture. But the surrounding it captures annoys you.

remove image background python

You have thought in your mind that wish I could remove that background and google the image background remover. You found that there are online tools that are available but you wish to automate those photo uploading and saving parts.

Thank’s to the open-source library called rembg which solves these problems with a few lines of Python codes.

Rembg is a utility for removing the background from photographs.

You can install the library using the pip installer as:

CPU intensive

$ pip install rembg

GPU intensive

$ pip install rembg[gpu]

Remove Image Background

Let’s dive into the coding part of this problem. We will be removing the image background of the image shown below as an example. In the image, you can see the elephant family walking in the jungle.

Elephant family walking in the jungle
Photo by Rohit Varma on Unsplash

By using the “rembg” library, we can remove the elephant family walking background and only capture the three elephants.

# import remove function from 
# rembg module
from rembg import remove
def remove_background(source_image_path, destination_image_path):
    try:
        with open(source_image_path, 'rb') as i, open(destination_image_path, 'wb') as o:
            # read source image in binary format
            input = i.read()
            # apply remove function to remove background
            output = remove(input)
            # save the new image in destination path
            o.write(output)
        print("Image background removed successfully")
    except Exception as e:
        print(f"There is problem while processed -> {e}")
# call function
remove_background("elephant_family_walking.jpg", "elephants_only.jpg")

Output:

Image background removed successfully

When you run the above python code, the file named ‘elephants_only.jpg’ was created in your working directory.

When you open that file, you can see the following image of the elephant family only:

image background removed by rembg python library

We can achieve the same output by using the “rembg” command-line interface(CLI).

Using CLI

rembg i "elephants_in_jungle.jpg" "elephants_only.jpg.jpg"

Conclusion

Here, in this article, we have gone through the python third-party library “rembg” in order to remove the background of the image successfully. We implemented the process in two ways, one through coding and another through a command-line interface.

If you have any questions, leave them in the comments. I’ll try my best to get back to you.

Take care and Happy learning!

If you like this, check out more blogs on Python:

Top Python Interview Questions for Freshers

Download Youtube Videos Using Python Pytube

How to Zip and Unzip Files in Python

Create Simple REST API with Django

Leave a Comment