It's done! I use 20 lines of python code to fix the certificate photo for the goddess next door, and then...

Posted by musson on Thu, 10 Feb 2022 07:42:56 +0100

Hello, I'm lex. I like bullying Superman. Lex

I suggest you collect it. In the future, you can help Miss P's photos, ID photos, size adjustment, background and matting. You can do it directly in 10 lines of code and get it up in an instant.

it happened like this

In the evening, I was concentrating on writing code

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

There is also a certificate to take photos of yourself

And it can be put on the marriage certificate


 

I've helped her several times before

Do you want to apply for certificates together

It turns out that the photo size is not appropriate

Let me fix the map for her. What else do you want with blue background and red background

Although a little lost

Still, I silently rolled out my 39 meter long python knife

 

First effect

1. Adjust the length and width to 295x413

2. The background tone is one blue background and one red background

3. And an ID photo with a transparent background.

Original drawing ↓↓↓

The effect is as follows ↓↓↓

 

Environmental preparation

Here, we need to use two python modules: pilot and removebg

Pilot module: used to adjust the pixel size of photos.

removebg module: used for matting and adjusting the background.

#Installing python modules
pip install pillow
pip install removebg

 

Size adjustment of certificate photo

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

Miss Shishi said that the photo size she required for the exam was 295x413

from PIL import Image

old_img = 'C:/Users/lex/desktop/img/Poetry.png'
new_img = 'C:/Users/lex/desktop/img/Poetry-new.png'
img = Image.open(old_img)
#Read photo size
(x,y) = img.size
#Resize photos
x_s = 295 #wide
y_s = 413 #high
out = img.resize((x_s,y_s),Image.ANTIALIAS) #resize image with high-quality
out.save(new_img)

print ('Original photo size(wide x high): ',x,"x",y)
print ('Adjusted photo size:(wide x high) ',x_s,"x",y_s)

The photo size has been adjusted

As shown in the following figure ↓↓

Background adjustment of ID photo

1. Through the method of removebg module, we can pull out the portrait.

2. We define three background colors through color background

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

3. Paste the extracted picture without background onto the background board drawn by ourselves

#Old photo path, new photo path, no background photo path, color
def get_img_bg(old_img_path,new_img_path,no_bg_img_path,color):
    #Remove the background image and extract the photo
    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])  # The size of the background image is the same as that of the foreground image
    background.paste(foreground, mask=foreground)
    background.save(new_img_path)

if __name__ == '__main__':
    get_img_bg('C:/Users/pacer/Desktop/img/Poetry.png','C:/Users/pacer/desktop/img/Poetry_red.png','C:/Users/pacer/desktop/img/Poetry.png_no_bg.png','RED')
    get_img_bg('C:/Users/pacer/Desktop/img/Poetry.png','C:/Users/pacer/desktop/img/Poetry_blue.png','C:/Users/pacer/desktop/img/Poetry.png_no_bg.png','BLUE')

The code was executed and all the photos were taken

Various background color pictures

The original image, transparent background, blue background and red background images are all generated.

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': (255, 255, 255, 255)
}
#Call removebg module
rmbg = RemoveBg('ogNLSpKn7VFeeamhVn7yaEhu', 'error.log')

#Generate ID photo with red background
#Old photo path, new photo path, no background photo path, color
def get_img_bg(old_img_path,new_img_path,no_bg_img_path,color):
    #Remove the background image and extract the photo
    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/Poetry.png','C:/Users/pacer/desktop/img/Poetry_red.png','C:/Users/pacer/desktop/img/Poetry.png_no_bg.png','RED')
    get_img_bg('C:/Users/pacer/Desktop/img/Poetry.png','C:/Users/pacer/desktop/img/Poetry_blue.png','C:/Users/pacer/desktop/img/Poetry.png_no_bg.png','BLUE')

ending

After tapping the code for half an hour,

I sent the ID photo of P to my little sister

This time, the response is very strong

 

Recommended reading

python and Security Series

[penetration case] fishing at work and entering a strange website by mistake - the result was hijacked by XSS

[penetration test] python your TM is too skinny - you can record every move of the keyboard in just 30 lines of code

[penetration practice] I forgot the password of the goddess album. I only wrote 20 lines of code in Python~~~

[penetration test] password brute force cracking tool -- detailed explanation and actual combat of hydra

[penetration learning] Web Security penetration detailed tutorial + learning route + detailed notes [the most complete in the whole network + suggestions collection]

[penetration case] how to use ssh tool to connect the front desk little sister's "Xiaomi mobile phone" -- Mr. Lei looked at the direct call expert!!!

[penetration test] password brute force cracking tool -- detailed explanation and actual combat of hydra

[penetration test] powerful and dangerous camera search engine - SHODAN
 

pygame series articles

Let's learn pygame together. 30 cases of game development (II) -- tower defense game

Let's learn pygame together. 30 cases of game development (III) -- shooting alien games

Let's learn pygame together. 30 cases of game development (4) -- Tetris games

Let's learn pygame together. 30 cases of game development (V) -- Xiaole games

Let's learn pygame together. 30 cases of game development (6) -- alpine skiing games

 

Topics: Python pillow