Remove background from Images using Python and AI

Original Source Here

Remove background from Images using Python and AI

Remove the background from images without spending hours on photo editing apps using AI. Bye, Bye Green Screen?

Input and Output Image side-by-side

Artificial Intelligence(AI) has been developing at a very rapid speed, be it computer vision, NLP(Natural Language Processing), Speech Synthesis, Art creation… The list is endless. But can it remove backgrounds from your images?

Yes, It Can.

Introducing Rembg, A python library that allows you to remove backgrounds from images. It is very easy to install and use. You can use it from the command line as well as from python code. It is written in python and is available on GitHub at
It is available in CPU and GPU versions, so if you do not have a gpu you can still use this library.

Rembg Repository

I Will be using Google Colab to demonstrate the usage of this library.

(Google colab allows us to run code in the browser without installing anything locally.)

Google Colab Image

(if you are unfamiliar with colab or do not want to run the entire code, there is a link to a web app at the end of this article where you can upload your images directly and play around with the model. But I highly encourage you to play around with the code.)

Let’s Get Started.

Go to and Click on “New Notebook”.
You will see a new colab notebook open. Click on the first cell and enter the following command and hit run. This will install the ‘rembg’ library and all the other required libraries.

!pip install rembg

You should see something like this.

Installation of the library

This step will take around a minute to run.

The next step is to download the image from a given url so that we can use that as an input for rembg. The following code does this for you.

from google.colab import files as FILE
import os
import requests
img_data = requests.get(url).content
with open('input.jpg', 'wb') as handler:

This will download the image to google colab. Now use the following command-line command to call rembg to remove background from content/input.jpg and store the output as /content/image1_output_bg.jpg.

!rembg i  /content/input.jpg /content/image1_output_bg.jpg

Here is the input image we used.

Input Image

Here is the rembg Output for the image

Output Image

Voilaaa!! and here you have it, a python library that removes background from your images.

Here are a few more examples.

Input Image
Output Image
Input Image
Output Image
Input Image
Output Image

But how do we use our own images without running code?
Here is the link which allows you to drag and drop without running any code. To use it, simply upload your image and click submit. An image may take some time to process as it runs on the CPU.

Huggingface webapp

Here you have it, AI removing background from images.
If you like reading articles like these, support writers like me by becoming a medium member using this link.


Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: