Here’s the thing

In the evening, I was concentrating on writing code

Suddenly, I received a message from my little sister next door

And an id selfie

And the kind you can put on your marriage certificate

I’ve done her a few favors before

Do you want to go to work together with the body

It turned out that the photo didn’t fit

Let me touch up her picture. What about blue, red and all kinds of background

Though a little lost

Still, silently lifted out my 39 meters long Python knife

The first effect

1, size length and width adjustment: 295×413

2. The background color is blue and red

3. And a photo with a transparent background.

The original left left left

The operation results are as follows ↓↓↓

Environment to prepare

Here, we need two Python modules: Pillow and Removebg

Pillow module: Used to adjust the pixel size of a photo.

Removebg module: Used for image matting and background adjustment.

PIP install pillow PIP install removebgCopy the code

Adjust the size of certificate photo

Let’s adjust the size first, and then adjust the background color.

Shi Shi little sister said that the size of the photo required for her exam: 295×413

The from PIL import Image old_img = 'C: / Users/lex/desktop/img/poem. PNG' new_img = 'C: / Users/lex/desktop/img/poem - new. PNG' img = Image.open(old_img) # image.open (old_img) # image.open (old_img) # image.open (old_img) # image.open (old_img) # image.open (old_img Img. resize((x_s,y_s), image. ANTIALIAS) #resize Image with high quality out.save(new_img) print (' original photo size(width x height): ',x,"x",y) print (x_s,"x",y_s) print (x_s,"x",y_s)Copy the code

Pop pop operation, the photo size adjusted

The following figure left left left

Adjust the background of certificate photo

1. Using the removeBG module method, we can map out the portrait.

2. We define three background colors by color background

BACKGROUND_COLOR = {
    'RED': (255, 0, 0, 255),
    'BLUE': (67, 142, 219, 255),
    'WHITE': (255, 255, 255, 255)
}
Copy the code

3. Paste the picture without background into the background board we draw ourselves

Def get_img_bg(old_img_path,new_img_path,no_bg_img_path,color): def get_img_bg(old_img_path,new_img_path,no_bg_img_path,color): Rmbg. remove_background_from_img_file(old_img_path) foreground = image. open(no_bg_img_path) background = Image. New (' foreground ', foreground. Size, BACKGROUND_COLOR[color]) # Paste (foreground, mask=foreground) background. Save (new_img_path) if __name__ == '__main__': Get_img_bg (' C: / Users/pacer/Desktop/img/poem. PNG ', 'C: / Users/pacer/Desktop/img/poem _red. PNG', 'C: / Users/pacer/Desktop/img/poem. Pn g_no_bg.png','RED') Get_img_bg (' C: / Users/pacer/Desktop/img/poem. PNG ', 'C: / Users/pacer/Desktop/img/poem _blue. PNG', 'C: / Users/pacer/Desktop/img/poem. P ng_no_bg.png','BLUE')Copy the code

Pop pop code executes, all the photos are in

Various background color images

Original image, transparent background, blue background, red background all generated.

The complete code

from os import name from PIL import Image from removebg import RemoveBg BACKGROUND_COLOR = { 'RED': (255, 0, 0, 255), 'BLUE': (67, 142, 219, 255), 'WHITE': } # RMBG = removebg ('ogNLSpKn7VFeeamhVn7yaEhu', Def get_img_bg(old_img_path,new_img_path,no_bg_img_path,color): Rmbg. remove_background_from_img_file(old_img_path) foreground = image. open(no_bg_img_path) background = Image.new('RGBA', foreground.size, BACKGROUND_COLOR[color]) background.paste(foreground, mask=foreground) #background.show() background.save(new_img_path) if __name__ == '__main__': Get_img_bg (' C: / Users/pacer/Desktop/img/poem. PNG ', 'C: / Users/pacer/Desktop/img/poem _red. PNG', 'C: / Users/pacer/Desktop/img/poem. Pn g_no_bg.png','RED') Get_img_bg (' C: / Users/pacer/Desktop/img/poem. PNG ', 'C: / Users/pacer/Desktop/img/poem _blue. PNG', 'C: / Users/pacer/Desktop/img/poem. P ng_no_bg.png','BLUE')Copy the code

At the end

After banging away at the code for half an hour,

I put P good certificate photo, sent a little sister

This time, the reaction was strong

We can take the code to some treasure opened a certificate according to the shop, do not receive, on two pieces of it! All are business opportunities!

Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base