inpainting图像修复api总是报参数错误
乐码编程 发布于2021-05 浏览:2005 回复:2
0
收藏
import requests,json
import base64

# 1.获取token 
tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token'
params={'grant_type':'client_credentials',
     'client_id':'6ethzDNLbyVjoqlajoANAdG3',
     'client_secret':'m1DFi57NZIkiGyhAv04toblZorRypmx7'
}

response = requests.post(tokenUrl,params)
token = ''
if response.status_code==200:
    token = response.json()['access_token']
else:
    print('token请求状态码错误:',response.text)

# 2. 调用图片修复api
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/inpainting"

# 2.1 二进制方式打开图片文件
""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

# 加载图片,并进行base64编码
img = base64.b64encode(get_file_content('example.jpg')).decode('utf-8')

# 指定区域(???这个区域不知如何设置,总是报参数错误)
rect = [{'width':170, 'top':725, 'height':55, 'left':880}]  
params = {"image":img,"rectangle":rect}

request_url = request_url + "?access_token=" + token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)

# 执行post请求后,报错信息如 {"log_id":1389511586980298752,"error_msg":"missing required parameter(s)","error_code":282003}

# 2.2 保存图片
if response.status_code==200:
    json = response.json()
    try:
        data = base64.b64decode(json['image']) 
        with open("result.jpg", "wb") as f:
            content = f.write(data)
    except:
        print('api请求发生错误:',response.text)
else:
    print('api请求状态码错误:',response.text)
收藏
点赞
0
个赞
共2条回复 最后由知乎日报回复于2021-08
#3知乎日报回复于2021-08

解决了吗

0
#2月光贼1985回复于2021-05

解决了吗

 

0
TOP
切换版块