百度AI平台人脸动漫化python测试
程序员文章站
2022-05-21 23:28:12
...
获取百度API
百度转换风格网址
获取API Key(AK)
和Secret Key(SK)
获取token
import requests
import base64
# 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)
access_token = response.json()["access_token"]
使用
使用效果:
'''
人像动漫化
'''
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 二进制方式打开图片文件
f = open('P1.png', 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
# access_token = '24.16d047826d9d2d9913516d1b47f5afcd.2592000.1600504705.282335-22127935'
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:
# 保存文件
f = open('target.jpg', 'wb')
img = (response.json()['image'])
f.write(base64.b64decode(img))
上一篇: python之课后练习三