Today praised me by a colleague of certificates as much flatter than I am, the thought of was a primary school, then certificate as there is no doubt in my mind is the photo, one inch photos can face all the disadvantages, I was a small round face, a palm-sized, but on a mirror, I buy kagyu, big face plate, from the high school certificate according to become my heart the most pain, Both girls and boys want their photo to be the highlight.

There are requirements on the size and background color of these photos. Of course, there is no shortage of beauty. I recommend meitu Xiu Xiu for gay men

Celebrity photos will be booed before they are posted. Boobs are healthier and prettier. Ju Jingyi, a beauty known as “once in 4,000 years in China”!

Effect comparison diagram:



Implementation steps

There are two main tasks for us: To modify the background and size of the photo, we need to use the third-party library removeBG to modify the background, and PIL library to modify the size of the photo. The installation of these two libraries can be done using PIP Install removeBG and PIP Install Pillow.

useremovebgWe need another oneAPIThe key is obtained by: First, we open the link addresshttps://accounts.kaleido.ai/users/sign_upRegister an account and open it as shown below:

Account registration: fill in your email address and password, check the agreement and submit, and get the email verification link to complete the verification.

Once the account is registered, open ithttps://www.remove.bg/zh/profile#api-keyAddress Login your account, you can enter the following location:



We click the show button in the picture above to get the secret key.

Reference code

The code implementation is also relatively simple, using removeBG library to modify the background color of the photo, using PIL library to modify the size of the photo, the specific implementation is as follows:

PIL import Image from removebg import removebg # def change_bgcolor(file_in, file_out, api_key, color): rmbg = RemoveBg(api_key, 'error.log') rmbg.remove_background_from_img_file(file_in) no_bg_image = Image.open(file_in) x, y = no_bg_image.size new_image = Image.new('RGBA', no_bg_image.size, color=color) new_image.paste(no_bg_image, (0, 0, X, y), no_bg_image) new_image.save(file_out) def change_size(file_in, file_out, width, height): image = Image.open(file_in) resized_image = image.resize((width, height), Image.ANTIALIAS) resized_image.save(file_out)Copy the code

Effect display:

Finally, let’s look at the implementation





That’s it. Thanks for watching. I’m a programmer who likes to share,You can ask me when you have a problem +Python resource sharing.