编程语言
首页 > 编程语言> > python百度api人像动漫化接口

python百度api人像动漫化接口

作者:互联网

import requests
import base64

# 获取Access Token
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=(你的key)&client_secret=(你的密码)'
response = requests.get(host)
access_token = response.json()['access_token']

# 获取本地文件
path = input("Enter the image path that you want to convert: ")
f = open(path, "rb")
img = base64.b64encode(f.read())

# 调用API转换图片
params = {"image": img}
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)

# 储存图片
f = open(path + "PT2CC.jpeg", "wb")
f.write(base64.b64decode(response.json()['image']))
print("The converted image has been stored in " + path + "PT2CC.jpeg")
input("Press Enter to quit...")
f.close() 

 

标签:人像,python,image,base64,access,token,api,path,response
来源: https://blog.csdn.net/qq_37195257/article/details/117784184