Recently, animation special effects are very popular on certain sound, many people play animation portraits, my wife is no exception. Seeing how much she liked the special effects, I decided to make a photo manipulation tool so THAT I could have my wife’s animation head. Coding in order to quickly achieve our goals, we do not write their own picture processing procedures, we directly call baidu AI open platform provided by the interface to achieve our needs.

coding

This time we import the following two libraries

# -*- coding:utf-8 -*-
#author: Rookie xiaobai learning share
import requests, base64
Copy the code

We need to conduct the interface authentication of Baidu AI open platform, and the code is as follows:

# -*- coding:utf-8 -*-
#author: Rookie xiaobai learning share
Baidu AI open platform authentication function
def get_access_token(a):
    url = 'https://aip.baidubce.com/oauth/2.0/token'
    data = {
        'grant_type': 'client_credentials'.# fixed value
        'client_id': 'A3ppUrgl6H*******NjDN4Bb'.The API Key of the application built after registration on the open platform
        'client_secret': 'SqaeFpiPPC**********H1lsb0xO3w'  # Secret Key of the app you built
    }
    res = requests.post(url, data=data)
    res = res.json()
    access_token = res['access_token']
    return access_token
Copy the code

Since Baidu AI platform provides a variety of image processing methods, the request URL body is the same, but the parameters are different, so we need to process parameters for different processing methods, the code is as follows:

# -*- coding:utf-8 -*-
#author: Rookie xiaobai learning share
def get_config(a):
    img_before = input("Please enter the name of the image to be processed under the current folder:")
    process_action = [' '.'selfie_anime'.'colourize'.'style_trans']
    print("Support the following processing actions: \n1: for portrait animation \ N2: for image color \ N3: for image stylization")
    # Handling actions: Selfie_anime for portrait, Colourize image coloring, style_trans for image stylization
    i = int(input("Please enter the action to be processed:"))
    "" Cartoon style pencil style Color_pencil style warm Sugar painting style Wave Kanagawa Wave painting style lavender mononoke The gothic style of painting with a scream
    others = [' '.'cartoon'.'pencil'.'color_pencil'.'warm'.'wave'.'lavender'.'mononoke'.'scream'.'gothic']
    j = 0
    if process_action[i] == 'style_trans':
        print("The styles supporting the transformation are: \n\ 1: cartoon style \ N \ 2: pencil style \ N \ 3: colored pencil style \ N \ 4: colored sugar painting style \ N \ 5: Kanagawa Surf Style \ N \ 6: Lavender painting style \n\ 7: Exotic painting style \n\ 8: Scream painting style \n\ 9: Gothic oil Painting style \ N")
        j = int(input("Please enter the style type (number) you want to convert:"))
    return img_before,process_action[i],others[j]
Copy the code

After obtaining the image and processing parameters, we took this information to request baidu AI open platform

# -*- coding:utf-8 -*-
#author: Rookie xiaobai learning share
def image_process(img_before, img_after, how_to_deal,others):
    # three parameters of the function, one is the name of the file before transformation, one is the name of the file after transformation, both in the same directory, the third is the image processing ability selection
    request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/' + how_to_deal
​
    file = open(img_before, 'rb')  # Binary read images
    origin_img = base64.b64encode(file.read())  Encode the image base64
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    data = {
        'access_token': get_access_token(),
        'image': origin_img,
        'option': others
    }
​
    res = requests.post(request_url, data=data, headers=headers)
    res = res.json()
​
    if res:
        f = open(img_after, 'wb')
        after_img = res['image']
        after_img = base64.b64decode(after_img)
        f.write(after_img)
        f.close()
Copy the code

Finally, let’s write the main function call again

# -*- coding:utf-8 -*-
#author: Rookie xiaobai learning share
if __name__ == '__main__':
    # Select input information
    img_before, process_action, others = get_config()
    img_after = img_before.split('. ')  # Divide the original file name into lists
    img_after = img_after[0] + '_1. + img_after[1]  Add _1 to the original file name
    image_process(img_before, img_after, process_action,others)
    print('done! ')
Copy the code

Implementation effect

Original image:

Procedures to obtain

For those of you who don’t have Python and want to experience the effects of the tool, I packaged the code as an executable, which can be obtained by following the public account “newfangled xiaobai learning share” and “image processing program”. Interested in their own research partners want to obtain the source, pay attention to the public number “rookie xiaobai learning to share” reply “picture processing source” can be obtained.

If you also recognize the rookie xiaobai’s learning and sharing, please give me a look, like, pay attention to + praise, because of you, so I have been working hard.

Teach you how to build an automatic reply robot for wechat public account

Darling asked me to find her a PDF to word free tool, looking for a long time I decided to write her out ^-^

Python uses the cx_Oracle library to connect to the Oracle server

Use Python to batch process Excel spreadsheets, the results are fast and good, be the most beautiful person in the office

Mom no longer has to worry that I can’t find the way

The study of a person — the study of a group of lonely people — happiness