WechatIMG1.jpeg

想看自己的漫画脸?用不着下快手抖音,这篇文章的目的就是使用Python来实现人像动漫化的效果。

首先,这里是我们使用的人像动漫化API的主页:人像动漫化-百度AI开放平台

该API的接口文档在主页可以找到:人像动漫化帮助文档

帮助文档中已经给出了API调用的示例,这里的代码很多我们可以直接使用:

# encoding:utf-8
import requests
import base64
'''

人像动漫化

'''
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())

接下来,我们按照我们的需求对代码进行修改。

首先,[本地文件]处我们需要一个输入,这里从简,直接使用input():

# encoding:utf-8
import requests
import base64
# 获取本地文件
path = input("Enter the image path that you want to convert: ")
f = open(path, "rb")
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '[调用鉴权接口获取的token]'
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())

另外在结尾处,增加储存图片以及用户交互,这里注意,API返回的值有log_id和image(详见帮助文档中的返回说明),储存图片时要做的就是将image中的base64str解码成二进制编码:

# encoding:utf-8
import requests
import base64
# 获取本地文件
path = input("Enter the image path that you want to convert: ")
f = open(path, "rb")
img = base64.b64encode(f.read())
# 调用API转换图片
params = {"image":img}
access_token = '[调用鉴权接口获取的token]'
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
# 储存图片
f = open(path + "PT2CC.jpeg", "wb")
f.write(base64.b64decode(response.json()['image']))
print("The converted image has been stored in " + path + "PT2CC.jpeg")
input("Press Enter to quit...")

最后,我们需要调用鉴权接口获取access_token,这里需要你进入百度智能云管理中心创建人像动漫化的应用,以获取API Key以及 Secret Key,这些是我们调用鉴权接口必要的参数:


创建应用

调用鉴权接口的示例代码如下:

# encoding:utf-8
import requests
# client_id 为官网获取的AK, client_secret 为官网获取的SK
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:
print(response.json())

AK就是刚才获取的API Key,SK为Secret Key

稍作修改,我们就可以将调用鉴权接口加入到我们的程序中:

import requests
import base64
# 获取Access Token
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=5oPEsBCqj76dm7EaO2u55DKV&client_secret=****************'
response = requests.get(host)
access_token = response.json()['access_token']
# 获取本地文件
path = input("Enter the image path that you want to convert: ")
f = open(path, "rb")
img = base64.b64encode(f.read())
# 调用API转换图片
params = {"image": img}
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
# 储存图片
f = open(path + "PT2CC.jpeg", "wb")
f.write(base64.b64decode(response.json()['image']))
print("The converted image has been stored in " + path + "PT2CC.jpeg")
input("Press Enter to quit...")

这里隐去了我的SK,代码不能直接复制使用

至此,程序完工,我们可以那官网上的示例图来测试一下程序是否能够使用:


官网示例图

/usr/local/bin/python3.7 /Users/liuxizai/Documents/Code/PT2CC.py

Enter the image path that you want to convert: /Users/liuxizai/Desktop/757770D78BB742738C0D3A0794ED1345.jpeg

The converted image has been stored in /Users/liuxizai/Desktop/757770D78BB742738C0D3A0794ED1345.jpegPT2CC.jpeg

Press Enter to quit...

Process finished with exit code 0


运行结果

最后还提一点,你可以在params中添加type和mask_id参数,这样可以为动漫人物戴口罩。

另外,这个程序的使用方式我也写一下,因为我的同学就拿着我这个程序不知道怎么使(@tph 这个同学不指你,不用怼我):

程序显示Enter the image path that you want to convert:时,输入需要转换的图片的绝对路径,相对路径也可以,你问我路径怎么看?Windows你可以在详细信息中找到文件的目录路径,然后在路径后面加上文件名,Macos的话,显示简介后,直接复制里面的目录路径,同样在路径后加上文件名,输入到程序中,回车。

接下来等待一段时间,程序有了反馈之后,你就可以找到同目录的PT2CC图片。

我有两个月没有写文章了吧……

明天我要去华一高进行为期十三天的信息学封闭集训,我其实现在能想到很多想写的内容,不过……只能等集训结束了。

对了,上次那个apk反编译我正式宣布没有后续。