Hi everyone, I’m 33, one who just finished lunch and is wasting his life by surfing the web…

The web content of a pile of boring gossip information slowly blurred my eyes, a yawn out three jin old tears, just then I saw a picture:

Who is it! Who put out the picture of my girlfriend?!

Awsl! It’s so beautiful…

Wait, what the hell is that bunch of doodles in the background? ! It’s hard to watch!

Uncle and aunt can endure, next door Lao Wang’s third aunt’s fourth cousin’s distant cousin’s classmate’s brother-in-law is still nursing nieces can not endure!

One-click download, one-click dump:

Get out of here, who’s afraid of who…

I, on my own!

After my unremitting efforts and in the spirit of shameless search spirit, the warm-hearted days, hardships, three thousand more armour can swallow Wu, was finally found by me…

Python will do it! I decided to share it! There is no need to ask for a picture cutout!

0x00 Preparations

1 You need to know a website:

https://www.remove.bg/

It opens like this:

It has released a tool for removing background color based on Python, Ruby and deep learning. It can use Python to call its API to remove background and leave foreground body.

2. Create an account

To get the API, you first need to have an account, which is easy to apply for, email.

If you are bothered to register, you can chat with me privately and use my API. With the API, you can only handle 50 images per day, first come, first served…

3 for API

Log in, first click on ‘Tools & API’, second click on ‘API Docs’ :

Step 3 click ‘Get API Key’ :

Step 4: Click ‘Show’ and your own API appears:

0x01 Official Image Matting

1 Download modules

Download the ‘removebg’ module from the official website:

pip install removebgCopy the code

2 three lines of code to achieve matting:

From removebg import removebg RMBG = removebg (' DHXXXXXXXXXXXXXXXX ', 'error.log') The second argument is to output the error to the log file RMBG.remove_background_from_img_file ('girl.jpg') # the image address is in parenthesesCopy the code

Image output is ‘girl.jpgnobg.png’, let’s see what it looks like:

Hee hee, the effect is very good…

It also supports batch operations:

From removebg import removebg import OS RMBG = removebg (' DHXXXXXXXXXXXXXXXX ', 'error.log') Path = f'{os.getcwd()}/imgs' # for img in os.listdir(path): Rmbg.remove_background_from_img_file (f'{path}/{img}') #Copy the code

Of course, if you feel that writing code is very troublesome, you can also operate directly on the website, or download its client, there is no limit to the number of times!

For example, on the website, simply click ‘Upload Image’ to operate:

Or download the client for Windows/Mac/Linux:

Although this is easier to use directly on the website or download the client, but as a Python number I still hope you use the code to try it out, it is really flying general feeling!

Get something out of it? Share so that more people can see ~ thank you

Finally, I would like to remind you that if you need API, you can forward this article and reply to “API” for free

Click here to learn more about Python

To learn more