1.接口说明
手动去水印/图片修复 API:根据用户提供的蒙版/矩形/涂抹笔触区域,对指定区域进行内容填充与修复,输出修复后的结果图片(Base64)。
1.1主要功能
- 蒙版修复:
- 上传灰度 mask,白色区域表示需要修复,黑色区域表示不修复。
- 矩形标注:
- 直接用多个矩形区域指定需要修复的范围。
- 涂抹笔触:
- 支持多笔 stroke,既可添加修复笔触,也可擦除已有笔触。
- 复用图片ID:
- 首次上传返回
image_id,后续仅传 id 与新蒙版即可反复调整,节省带宽与耗时。
1.2接入场景
电商主图去水印、图片瑕疵修补、文字/Logo清理、老照片修复、设计素材清理等。
2.请求信息
2.1请求地址(URL)
POST https://api.shiliuai.com/api/inpaint/v1
2.2请求方式
POST
2.3请求头(header)
| 参数 | 类型 | 说明 |
|---|---|---|
| Content-Type | string | application/json |
| APIKEY | string | 您的 API KEY |
2.4请求体(body)
| 参数 | 是否必填 | 类型 | 说明 |
|---|---|---|---|
| image_base64 | 必须填写其中之一 | string | base64编码的图片文件,必须提供image_base64或image_id其中一个, 图片文件要小于20M,图片的长边不能超过4096像素 |
| image_id | string | 图片id,对于已经请求过的图片,如果需要改变蒙版,可以使用id而无需再上传image_base64 | |
| mask_base64 | 必须填写其中之一 | string | base64编码的蒙版文件,必须提供mask_base64或rectangles或strokes的其中一个, 蒙版图片为灰度图,黑色区域表示不需要修复,白色区域表示需要修复 |
| rectangles | list | 矩形区域,可以是多个,格式为[{"x1":x1, "y1":y1, "x2":x2, "y2"y2}] | |
| strokes | list | 涂抹笔画,可以是多笔,格式为[{'type':stroke_type, 'thickness':thickness, 'points':points}], 其中stroke_type=1表示修复笔触,stroke_type=0表示清除修复笔触, points=[[x0, y0], [x1, y1], ...] | |
| mode | 否 | string | 蒙版,矩形,涂抹的添加模式,"new": 重新绘制,"add": 在已有结果上添加,默认为"new" |
3.返回信息
3.1返回类型
JSON
3.2返回信息
| 参数 | 说明 |
|---|---|
| code | 错误码 |
| msg | 错误信息(英文) |
| msg_cn | 错误信息(中文) |
| result_base64 | 修复后的图片的base64编码,jpg格式,(当code==0时会有该返回值) |
| image_id | 图片id,(当code==0时会有该返回值) |
3.3返回示例
{
"code": 0,
"msg": "OK",
"msg_cn": "成功",
"result_base64": "/9j/4AAQSkZJRgABAQAAAQABAAD...",
"image_id": "b6a0f7d0b2f54d0ea3..."
}
// 失败示例
{
"code": 4,
"msg": "Invalid parameter: mask_base64 / rectangles / strokes is required",
"msg_cn": "参数错误:mask_base64 / rectangles / strokes 必须填写其中之一"
}
3.4错误码
| 错误码 | 说明 |
|---|---|
| 0 | 成功 |
| 1 | 图片错误 |
| 2 | 处理错误 |
| 3 | 服务器繁忙 |
| 4 | 参数错误(具体错误看 msg 或 msg_cn) |
| 5 | 未知错误 |
| 101 | API-KEY 不正确 |
| 102 | 未知用户 |
| 103 | 积分已用完 |
| 104 | 扣除积分失败 |
4.示例代码
4.1 Python
# -*- coding: utf-8 -*-
import requests
import base64
import cv2
import json
import numpy as np
api_key = '******' # 你的API KEY
image_path = '...' # 图片路径
mask_path = '...' # 蒙版路径
"""
第一次用 image_base64 请求,用 mask_base64
"""
with open(image_path, 'rb') as fp:
image_base64 = base64.b64encode(fp.read()).decode('utf8')
with open(mask_path, 'rb') as fp:
mask_base64 = base64.b64encode(fp.read()).decode('utf8')
url = 'https://api.shiliuai.com/api/inpaint/v1'
headers = {'APIKEY': api_key, 'Content-Type': 'application/json'}
data = {
'image_base64': image_base64,
'mask_base64': mask_base64
}
response = requests.post(url=url, headers=headers, json=data)
response = json.loads(response.content)
"""
成功:{'code': 0, 'msg': 'OK', 'msg_cn': '成功', 'result_base64': result_base64, 'image_id': image_id}
失败:{'code': error_code, 'msg': error_msg, 'msg_cn': '错误信息'}
"""
image_id = response.get('image_id')
result_base64 = response.get('result_base64')
if result_base64:
file_bytes = base64.b64decode(result_base64)
with open('result.jpg', 'wb') as f:
f.write(file_bytes)
image = np.asarray(bytearray(file_bytes), dtype=np.uint8)
image = cv2.imdecode(image, cv2.IMREAD_UNCHANGED)
cv2.imshow('result', image)
cv2.waitKey(0)
"""
第二次用 image_id 请求,用 rectangles 参数
"""
rectangles = [
{'x1': 298, 'y1': 250, 'x2': 450, 'y2': 306},
{'x1': 616, 'y1': 519, 'x2': 732, 'y2': 560}
]
data = {
'image_id': image_id,
'rectangles': rectangles
}
response = requests.post(url=url, headers=headers, json=data)
4.2 PHP
$url = "https://api.shiliuai.com/api/inpaint/v1"; $method = "POST"; $apikey = "******"; $header = array(); array_push($header, "APIKEY:" . $apikey); array_push($header, "Content-Type:application/json"); $image_path = "..."; $handle = fopen($image_path, "r"); $image = fread($handle, filesize($image_path)); fclose($handle); $image_base64 = base64_encode($image); $mask_path = "..."; $handle = fopen($mask_path, "r"); $mask = fread($handle, filesize($mask_path)); fclose($handle); $mask_base64 = base64_encode($mask); $data = array( "image_base64" => $image_base64, "mask_base64" => $mask_base64 ); $post_data = json_encode($data); $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($curl); var_dump($response);
4.3 C#
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string apiKey = "******"; // 你的API KEY
string imagePath = "..."; // 原图路径
string maskPath = "..."; // 蒙版路径
string url = "https://api.shiliuai.com/api/inpaint/v1";
// 将原图编码为Base64
string imageBase64 = await EncodeFileToBase64Async(imagePath);
// 将蒙版编码为Base64
string maskBase64 = await EncodeFileToBase64Async(maskPath);
// 构造请求数据
var requestData = new
{
image_base64 = imageBase64,
mask_base64 = maskBase64
};
string jsonData = JsonSerializer.Serialize(requestData);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("APIKEY", apiKey);
client.DefaultRequestHeaders.Add("Content-Type", "application/json");
try
{
// 发送POST请求
var response = await client.PostAsync(url, new StringContent(jsonData, Encoding.UTF8, "application/json"));
string responseString = await response.Content.ReadAsStringAsync();
// 解析响应
var responseObject = JsonSerializer.Deserialize<JsonElement>(responseString);
int code = responseObject.GetProperty("code").GetInt32();
if (code == 0)
{
string resultBase64 = responseObject.GetProperty("result_base64").GetString();
// 将Base64转换为图片并保存
byte[] fileBytes = Convert.FromBase64String(resultBase64);
File.WriteAllBytes("result.jpg", fileBytes);
Console.WriteLine("Image processing succeeded, saved as result.jpg");
}
else
{
string errorMsg = responseObject.GetProperty("msg_cn").GetString();
Console.WriteLine($"Error: {errorMsg}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}
private static async Task<string> EncodeFileToBase64Async(string path)
{
using (var stream = File.OpenRead(path))
{
byte[] bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
return Convert.ToBase64String(bytes);
}
}
}