Software and Hardware Environment

  • windows 10 64bit
  • Pytorch 1.7
  • rembg

Introduction to the

Rembg is a Python tool to remove the background of images. It is not only available in Python, but also provides an executable program, which is very convenient.

The installation

Install directly through PIP

pip install rembg
Copy the code

Or pull the source code from Github and install it

git clone
cd rembg
pip install -r requirements.txt
Copy the code

Once installed, the REMbg executable is generated, which you can then use for testing

Using the command line

First download the model file. Here are two addresses:

  • Google Web disk links

  • Baidu network backup link:… , extract code: LGUg

After the file is downloaded, copy it to the ~/.u2net folder. By default, the folder does not exist. You need to create the folder by yourself

Now you can test with your local images

rembg -o output_test.jpg test.jpg
Copy the code

-o indicates the processed image

In addition to working with individual images, Rembg also supports folder processing, where the above two images are placed in a folder and then the command is used

rembg -p test
Copy the code

Rembg also provides the Interface on the Web side to run the rembg-server to start the back-end service, which uses the Flask framework

Then type it in your browser
Copy the code

After url= is the address of the image, you can see that Rembg supports HTTPS

Call rembg in the code

Look at the following example

import sys
from import remove

Copy the code

It is then executed in the terminal

cat bryant.jpg | python > output.png
Copy the code

The code runs the same as above

The resources