Skip to content
site logo mobile

Remove Background From Image With Python For Free!

Table of Contents

remove background from image with python

I was surfing the internet looking for AI tools and methods to make money online when I stumbled upon an Image Background Removal service called Remove.Bg

It works great. But I was shocked by the pricing. It charges $0.2-$1.99 per Image!?

remove background from image removebg

So, I decided to take the challenge and reverse engineer this website to build my own Free Image Background Remover without relying on any APIs or Third Parties. So, I searched for a few days and found a great Python project on GitHub called U2Net.

A Big Shout-out and Thanks to the authors of this project!

I tried my best to make the Python script I built as simple as possible so that even newbies could use and run it.

I also created a Short Video showing The Script & How to use it, and I pointed out three methods and secrets that may help you build an online income stream with the help of this script. Here is the video:

You can make money using this tool by turning it into an online tool that people pay to use based on a monthly subscription. Or, you can turn the script into an API, publish it on RapidAPI, and sell it for a monthly recurring income as I do with my APIs.

Remove Background from Image with Python

The main part of our script is this magic method called removeBg(). We’ll pass the Image Path as an argument.

Two methods are very important in this Python Script. One saves the output, and the other will create the mask of the image, the image without background, and returns it.

In the first part, we load the model. (I will also give you the trained model so you can use it directly.) Then, we call the removebg() function to remove the background image.

Let’s see how it works. I will reopen and get a sample image from their website so we can compare the results.

I will save this cat image on my desktop.

I will copy the name, open the script again, and paste the image path here:

Now, run the script, and to see the results, go to your Python Project Folder.

Find a folder named ‘static’ and go to inputs in this folder.

So, this is the input (this is the main image.)

The Masks folder will have a mask of the same image.  

And finally, this is the result. It’s Great!

Now let’s go to The result here is almost the same!

Source Code

Also, here is some help with the file structure if you download the Full Project Folder:

When you open the Downloads folder. You’ll find this inside:

Saved models


And here is the trained model so you can use it directly. 

And we have the script file here.

The inputs, masks, and results are in the static folder. You can also find them here. It’s super easy to use.

You can use this requirements text file directly to import and install all the requirements with the pip command.

If you have any questions, please share them in the comments below.

91 thoughts on “Remove Background From Image With Python For Free!”

  1. Dear Mr. Hassan.

    Thank you for sharing your ideas online for free. Your generosity and selflessness are genuinely appreciated.
    I would be much grateful if you could make a detailed video on creating an online tool with the script you gave.

    Best regards,
    From Ghana

  2. Hi you are doing a treasure hunt. Thanks so much and very appropriate to you for giving it free to us.

  3. “You can use this requirements text file directly to import and install all the requirements with the pip command.”

    There’s no text file?

Leave a Reply

Your email address will not be published. Required fields are marked *