1:百度云申请
https://console.bce.baidu.com/ai/#/ai/imageprocess/app/list
2:python代码实现
import requests
import pprint
import base64
def get_access_token(id, secret):
get_access_token_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + id + '&client_secret=' + secret
response = requests.get(get_access_token_url)
content = response.json()
access_token = content['access_token']
return access_token
def Animation(img_file, access_token):
request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
f = open(img_file, 'rb')
image = base64.b64encode(f.read())
params = {"image": image}
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)
image_content = response.json()
image = image_content['image']
with open('result.jpg', 'wb') as f:
f.write(base64.b64decode(image))
def main():
img_file = 'ysp.png' # 图片地址
id = 'hwgOvS2PqYdHh07EEqU3wGYSPmy'
secret = 'aD3L3yoLSTE9UFqC6C3ShalAM3r4KY95YSP'
access_token = get_access_token(id, secret)
Animation(img_file, access_token)
if __name__ == '__main__':
main()
3:效果
原始图
效果图