Python + AI: perfect cutout, change background

Looking through the old photos at home, I found a little dog. It’s very interesting, with its wide eyes~ (the background is in the kitchen).

Sometimes you want to cut out the characters or protagonists in certain photos and paste them into your favorite background image. For example, you want to make a DIY avatar yourself.

Although there are many APPs or small programs that can provide such functions, it is no problem to pay for the service, but the free services have flaws. For example, the final output image has an advertising watermark, or the pixels are too low, causing the image to appear slightly Blur and so on.

If you want free breakfast, lunch and dinner, I understand. It’s better to ask for help than to ask for help from others. Let’s use our geek spirit and let’s do it ourselves. Next, I will introduce a very easy-to-use python toolkit Rembg, which also uses AI such as large models to assist in extracting portraits or protagonists. See below for details.

Environment preparation-linux

Rembg is developed in python, relies on the python3 execution environment, and requires a version between python 3.7 ~ 3.11.

The platform I often use is linux. Most of the linux distributions now have python3 installed by default. It is better to confirm the python version before starting.

$ cat /etc/issue
Ubuntu 20.04.4 LTS
$ python -V
Python 3.8.10

If you installed python 2 first and then installed python 3, then you can

$ python3 -V

Environment

Before installing the toolkit, it is necessary to remind you that each tool has a specific environment that it depends on, so it is necessary to isolate the environments from each other. In the python ecosystem, we can use the characteristics of the virtual environment to do this point.

Create a project directory rembg, and create a new virtual environment in it. The directory name for storing the environment is set to .venv, and then switch the python environment to the virtual environment.

$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $

After entering the virtual environment, the directory of the virtual environment will be automatically added to the front of the command line, indicating that you have entered the virtual environment in that directory. For more information about the python virtual environment, you can read my other article “Python: Flexible Development Environment” // TODO.

Install Rembg

(.venv) $ pip install rembg

Rembg has many dependency packages. After installation and startup, you need to wait for a while. We will come back after a cup of tea. . .

If your network environment is not very smooth, you can refer to the method on the Internet, switch the Linux installation package source to the mirror source in the wall, do not expand it, and check it yourself.

Although the pre-run environment has been installed, Rembg will download a pattern recognition image processing model called U-2-Net (U square net) from github when it is run locally for the first time. This is a model based on the Apache 2.0 license. The open source AI project has won the 2020 BEST PAPER AWARD.

At this point, if the execution still fails, it is most likely because your network is restricted and the download fails, such as inside a wall. Don’t panic, I’ve thought of it for you. You can reply to [u2net] in the background of my official account to get the network disk sharing link. After the download is completed, use tar -zxf to decompress and store it in the current user’s ~/.u2net/u2net.onnx. u2net.onnx is the final file after decompression. The size is close to 200 MB. It takes a while to download, but this is a small case for you who are extremely curious.

Try it off – erase the background

Okay, the environment is ready, now let’s start playing with the newly sharpened sword

Animals

Let’s try it with the dog picture from the beginning of the article. The original picture is dog.jpeg. The output picture is dog_without_bg.jpeg without background. The file paths are all in the current folder.

(.venv) $ rembg i dog.jpeg dog_without_bg.jpeg
(.venv) $ll
...
-r-xr-xr-x 1 root root 959306 Mar 16 2017 dog.jpeg*
-rwxrwxrwx 1 root root 2088784 Oct 21 20:52 dog_without_bg.jpeg*

Open the output image dog_without_bg.jpeg and take a look

It seems that the image extraction is very good, the background is really erased, and the speed is very fast. The size of the input source image file dog.jpeg is close to 1 MB, and it only takes more than ten seconds. Maybe my machine is more advanced? In fact, the hardware is quite satisfactory now, and you don’t need to worry about the hardware at all.

Characters

After trying the animals, try another portrait and try the moment when the cute little girl was secretly photographed.

(.venv) $ rembg i girl.jpeg girl_without_bg.jpeg
(.venv) $ll
...
-r-xr-xr-x 1 root root 99541 Dec 28 2014 girl.jpeg*
-rwxrwxrwx 1 root root 857075 Oct 21 20:54 girl_without_bg.jpeg*

Open the output picture girl_without_bg.jpeg and take a look

The effect is really impressive. The size of the input image file girl.jpeg is close to 100 KB, and it only takes a few seconds. It’s great!

Change background

The above only erases the background in the old photo. The new background insertion requires other software to complete, and the inserted picture can be added through the layer operation of professional image processing software, such as Photoshop and so on.

But I want to introduce a very easy-to-use open source free software to everyone, so that you don’t need to worry about pirated licenses. Why do I like to worry about everyone so much?

GIMP Challenge Accepted! GIMP is cross-platform and has complete support on all mainstream operating systems.

Let’s take a look at the effect first. I changed the background of the puppy to a more festive occasion. You see

how did you make it?

  1. Open GIMP, then the menu bar File –> Open, and select the dog_without_bg.jpeg output by Rembg above with the background erased.
  2. Go to the menu bar again File –> Open as layer and select a photo that you think is very festive. This photo will be used as the new background image. I chose a photo after setting off firecrackers. Firecrackers symbolize excitement, which couldn’t be better.
  3. At this time, you will find that the new background image is at the front, covering all other content. You can have a layer window on the right side of the main window, which lists two layers. The top is the new background image that was opened last, and the bottom is the puppy image without background, so you can drag it. Change the order of the two, and then you can see that the puppy is standing in the new background image.
  4. There are some later size and position adjustments, which are skipped here. . .

The rapid rise of Python is extremely beneficial to the entire industry, but “There are many popular people and not many people“, which has led to a lot of criticism, but it still cannot stop its popularity. development momentum.

If you are interested in Python and want to learn Python, here I would like to share with you a Complete set of Python learning materials, which I compiled during my own study. I hope it can help you, let’s work together!

Friends in need can click the link below to get it for free or Scan the QR code below to get it for free
Python complete set of learning materials

1Getting started with zero basics

① Learning route

For students who have never been exposed to Python, we have prepared a detailed Learning and Growth Roadmap for you. It can be said to be the most scientific and systematic learning route. You can follow the above knowledge points to find corresponding learning resources to ensure that you learn more comprehensively.

② Route corresponding learning video

There are also many learning videos suitable for beginners. With these videos, you can easily get started with Python~

③Exercise questions

After each video lesson, there are corresponding exercises to test your learning results haha!

2Domestic and foreign Python books and documents

① Documents and books

3Python toolkit + project source code collection

①Python toolkit

The commonly used development software for learning Python is here! Each one has a detailed installation tutorial to ensure you can install it successfully!

②Python practical case

Optical theory is useless. You must learn to type code along with it and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases. 100+ practical case source codes are waiting for you!

③Python mini game source code

If you feel that the practical cases above are a bit boring, you can try writing your own mini-game in Python to add a little fun to your learning process!

4Python interview questions

After we learn Python, we can go out and find a job if we have the skills! The following interview questions are all from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. I believe everyone can find a satisfactory job after reviewing this set of interview materials.

5Python part-time channel

Moreover, after learning Python, you can also take orders and make money on major part-time platforms. I have compiled various part-time channels + part-time precautions + how to communicate with customers into documents.

All the above information , if friends need it, you can scan the QR code below to get it for free