Realize avatar animation python
first step:
Obtain an interface from the AI smart platform provided by Baidu or Tencent, that is, an exclusive key
The following is the process for Baidu to obtain this:
just register an account later.
Step 2: Install two modules (base64 and requests)
When installing the module, it is recommended to download the mirror
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple base64
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
The third step: write function function
(1) Interface calling method
def get_access_token():
# 获取token的API
url = 'https://aip.baidubce.com/oauth/2.0/token'
# 获取access_token需要的参数
params = {
# 固定参数
'grant_type': '',
# 必选参数,传入你的API Key
'client_id': '',
# 必选参数,传入你的Secret Key
'client_secret': ''
}
# 发送请求,获取响应数据
response = requests.post(url, params)
# 将响应的数据转成字典类型,然后取出access_token
access_token = eval(response.text)['access_token']
# 将access_token返回
return access_token
(2) Picture decoding output
def img2Cartoon(img):
# 头像动漫化的API的网址
url = ''
# 以二进制的方式读取原始图片
origin_im = open(img, 'rb')
# 将图片进行base64编码
img = base64.b64encode(origin_im.read())
# 关闭原图片
origin_im.close()
# 请求的headers信息,固定写法
headers = {
'content-type': 'application/x-www-form-urlencoded'}
# 请求的参数
params = {
# 开始获取的access_token
'access_token': get_access_token(),
# 图片的base64编码
'image': img,
}
# 发送请求
response = requests.post(url, data=params, headers=headers)
# 对响应结果进行处理
if response:
# 打开一个文件
f = open('resultt111.jpg', 'wb')
# 获取动漫头像
anime = response.json()['image']
# 对返回的头像进行解码
anime = base64.b64decode(anime)
# 将头像写入文件当中
f.write(anime)
f.close()
Function call
if __name__ == '__main__':
img2Cartoon('1.jpg')