某音爆火,人物头像动漫化,为女朋友打造独一无二的头像
大家好,我是辣条。
我会尽量保持一周三更的进展,大家有什么好的建议可以在评论中交流哦,如果这篇文章对你有帮助的话,记得给辣条三连哦!
实现目标效果
工具使用
开发环境:win10、python3.7
开发工具:pycharm、Chrome
工具包:requests,basse64
接口使用:百度AI
项目思路解析
使用百度大脑提供的接口进行头像转换
查阅当前api的接口文档
HTTP 方法:POST 请求URL: https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime
url参数需要额外生成
clinet_id和cleint_secret为创建的百度ai应用的数据 在控制台创建
简易源码分享
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)