一、接口描述
智能识别黑白图像内容并填充色彩,使黑白图像变得鲜活。
二、使用攻略
-
请求说明
请求示例
HTTP 方法:
POST
请求URL:
https://aip.baidubce.com/rest/2.0/image-process/v1/colourize
URL参数:
参数 值 access_token 通过API Key和Secret Key获取的access_token,参考”Access Token获取” Header如下:
参数 值 Content-Type application/x-www-form-urlencoded Body中放置请求参数,参数详情如下:
请求参数
参数 是否必选 类型 可选值范围 说明 image true string - base64编码后大小不超过4M,最短边至少64px,最长边最大800px,长宽比3:1以内。注意:图片的base64编码是不包含图片头的,如 (data:image/jpg;base64,)
-
返回说明
返回参数
字段 是否必选 类型 说明 log_id 是 uint64 唯一的log id,用于问题定位 image 否 string base64编码图片 返回示例
{ "log_id": "6876747463538438254", "image": "处理后图片的Base64编码" }
-
源码共享
# encoding:utf-8 import requests import base64 #获取access_token client_id = ' ' # client_id 为官网获取的AK client_secret = ' ' #client_secret 为官网获取的SK host = '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()) token = response.json()['access_token'] #黑白图像上色 request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize" f = open('black.jpg', '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()) img = base64.b64decode(response.json()['image']) file = open( 'result.jpg', 'wb') file.write(img) file.close
三、效果测试