请求URL:
URL参数:
Header如下:
Body中放置请求参数,参数详情如下:
请求参数
3.8 在线调试API
大家可以进入在线调试接口,嫌麻烦的也可以跳过这一步,直接通过代码调试
3.9 python调用
官方给出的代码如下:
# encoding:utf-8
import requests
import base64
'''
人像动漫化
'''
request_url = \”https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime\”
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
params = {\”image\”:img}
access_token = '[调用鉴权接口获取的token]'
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)
if response:
print (response.json())
我们对其稍作修改
# encoding:utf-8
import requests
import base64
def image_process(img_name,client_id,client_secret):
request_url = \”https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime\”
# 二进制方式打开图片文件
f = open(img_name, 'rb')
img = base64.b64encode(f.read())
params = {\”image\”:img}
access_token = get_token(client_id,client_secret)
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)
if response:
print (response.json())
return response.json()['image']
3.10 获取token
官方给出的token获取方式:
# encoding:utf-8
import requests
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
response = requests.get(host)
if response:
print(response.json())
我们对其稍作修改
def get_token(client_id,client_secret):
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}'
response = requests.get(host)
if response:
print(response.json())
return response.json()['access_token']
else:
print('no response!')
然后本地图片url怎么获取,运行代码就可以获取处理后图片的base64编码啦~
# client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id='xxx'
client_secret='xxx'
img_name='img_name.jpg'
img_base64=image_process(img_name,client_id,client_secret)
print(img_base64)
可以发现,官方已经给出了很详细的代码模板本地图片url怎么获取,我们真正需要改的部分也就只有
client_id='xxx'
client_secret='xxx'
两行而已~
3.11 查看图片
获取到编码后,可以在浏览器输入data:image/jpeg;[你的base64编码,注意需要去掉中括号]查看图片
或者使用下面的代码将base64编码转换成图片
def base2picture(img_base64,img_save):
imgdata = base64.b64decode(img_base64)
with open(img_save,'wb') as f:
f.write(imgdata)
img_save='img_save.jpg'
base2picture(img_base64,img_save)
4 完整代码
# encoding:utf-8
import requests
import base64
def image_process(img_name,client_id,client_secret):
request_url = \”https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime\”
# 二进制方式打开图片文件
f = open(img_name, 'rb')
img = base64.b64encode(f.read())
params = {\”image\”:img}
access_token = get_token(client_id,client_secret)
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)
if response:
print (response.json())
return response.json()['image']
def get_token(client_id,client_secret):
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}'
response = requests.get(host)
if response:
print(response.json())
return response.json()['access_token']
else:
print('no response!')
def base2picture(img_base64,img_save):
imgdata = base64.b64decode(img_base64)
with open(img_save,'wb') as f:
f.write(imgdata)
if __name__=='__main__':
client_id='xxx'
client_secret='xxx'
img_name='1.jpg'
img_base64=image_process(img_name,client_id,client_secret)
print(img_base64)
# 或者在浏览器输入 data:image/jpeg;[base64]
img_save='2.jpg'
base2picture(img_base64,img_save)
大家都学会了吗?
快去试试吧~