We all know that in our life often need to use a variety of versions of electronic id photos, such as: red, blue, white, one inch, two inch and so on. In Python, you can change the background color of an image by using the image module. With the image module, you can change the size of the image, the background color, and so on.

As the separation of

The first step is to separate the figure in the original picture from the background. We use the portrait segmentation function in Baidu AI open platform, which has 50000 times per day for free. Baidu products are known to require an S and AK.

Here’s the code:

def get_access_token(self) :
    """ Get access_token """
    # pay attention to SK and AK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=ak&client_secret=sk'
    response = requests.get(host)
    if response:
        return response.json()['access_token']

def get_foreground(self, originalImagePath, ) :
    """ Like a partition """
    
    Open the image file in binary mode
    f = open(originalImagePath, 'rb')
    img = base64.b64encode(f.read())
    params = {"image": img}

    Request Baidu AI open platform
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=" + get_access_token()
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    params = {"image": img}
    response = requests.post(request_url, data=params, headers=headers)

    if response:
        foreground = response.json()['foreground']
        img_data = base64.b64decode(foreground)
        # Address for storing portrait pictures
        foreground_path = 'foreground.png'
        with open(foreground_path, 'wb') as f:
            f.write(img_data)
Copy the code

Results:

Then we will create the background image

The second step will create an image with a red, blue, and white background that is one inch (295px by 413px) and two inches (413px by 579px).

def get_background() :
    """ Background image """
    color = ('red'.'blue'.'white')
    imgs = []
    for c in color:
        # One-inch photo size
        img = Image.new("RGBA", (295.413), c)
        imgs.append(img)
    return imgs
Copy the code

The last step is to merge the images

The third step is to combine the red, blue and white background Image with the portrait Image. Here, resize() of the Image module is used to cut the portrait Image to an appropriate pixel, and then paste() is used to combine the Image.

def main() :
    fore = get_foreground('original.jpg')
    Crop the image to the appropriate pixel
    p = fore.resize((330.415))
    # Split images
    r,g,b,a = p.split()

    imgs = get_background()
    for img in imgs:
        # Merge the background image with the character image. Mask parameter is to remove the transparent background color
        img.paste(p, (-30.50), mask = a)
        img.show()
Copy the code

Results:

conclusion

The Image module can be used to make all kinds of electronic id photos we need. If the background picture is changed into a landscape picture, we can travel in the circle of friends.

The above is the content brought by Xiaobian today. Xiaobian is a Python development engineer. I spent three days to organize a set of Python learning tutorials, from the most basic Python scripts to Web development, crawlers, data analysis, data visualization, machine learning, etc. These materials can be obtained by clicking on the friends who want them