大家好,我是辣条。
我会尽量保持一周三更的进展,大家有什么好的建议可以在评论中交流哦,如果这篇文章对你有帮助的话,记得给辣条三连哦!
实现目标效果
工具使用
开发环境:win10、python3.7
开发工具:pycharm、Chrome
工具包:requests,basse64
接口使用:百度AI
项目思路解析
使用百度大脑提供的接口进行头像转换确定url的请求网址
<pre>HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime</pre>
url参数需要额外生成获取数据后提取json数据的image数据 base64解码 转换成图片进制数据
简易源码分享
import requests
import base64
url = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
'grant_type': 'client_credentials',
'client_id': '你的ak',
'client_secret': '你的sk'
}
response = requests.get(url, data)
access_token = response.json()['access_token']
print("access_token 的值为:", access_token)
requests_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
with open('./img/E6I(82]EI7$V2}3SDN(@UGN.png', 'rb') as file:
img = base64.b64encode(file.read())
params = {"image": img}
headers = {'content-type': 'application/x-www-form-urlencoded'}
requests_url = requests_url + "?access_token=" + access_token
response = requests.post(requests_url, data=params, headers=headers)
with open("001.jpg",'wb') as file:
anime = response.json()['image']
anime_image = base64.b64decode(anime)
file.write(anime_image)