# encoding:utf-8
import base64
import requests
imgPath = r'./微信图片_20210410135001.jpg'
filePath=r'./结果/.1.png'
def Get_access_token():
url = "https://aip.baidubce.com/oauth/2.0/token"
access_token = ''
parms = {
'grant_type': '',
'client_id': 'zwOTmcj5uw4Sf1HHWHsXtUvF', # API key
'client_secret': 'mHnSBN0Fb6KjfATpG4SOVvFGGN53dkUU' # Secret Key
}
res = requests.post(url, parms)
if res:
print(res)
print(res.json()['access_token'])
access_token = res.json()['access_token']
return access_token
def tx(access_token):
url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 要转换的图片
f = open(imgPath, 'rb')
img = base64.b64encode(f.read()) # 转码
parms = {
"image": img,
"access_token": access_token
}
headers = {'content-type': 'application/x-www-form-urlencoded'}
res = requests.post(url, parms, headers=headers)
return res
def get_picture(res):
if res:
with open(filePath, 'wb') as f:
image = res.json()['image'] # 获取返回json格式中image
image = base64.b64decode(image) # 解码
f.write(image)
f.close()
print("已完成")
if __name__ == '__main__':
access_token = Get_access_token()
res = tx(access_token)
get_picture(res)
import base64,requests,time
# host='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=zwOTmcj5uw4Sf1HHWHsXtUvF&client_secret=mHnSBN0Fb6KjfATpG4SOVvFGGN53dkUU'
# res = requests.get(host)
# if res:
# print(res.json())
# time.sleep(9999999)
url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
f = open('0001.jpg','rb')
img = base64.b64encode(f.read()) # 转码
params = {"image": img}
token='24.19d684a7c1c897c4dc1512ba63cb6c0c.2592000.1620643246.282335-23965337'
url=url+'?access_token='+token
headers = {'content-type': 'application/x-www-form-urlencoded'}
res = requests.post(url, data=params, headers=headers)
image=res.json()['image']
image=base64.b64decode(image)
with open(r'animation.png','wb')as f:
f.write(image)