在很多系统中,只要涉及用户上传图片,都会遇到一个问题:
图片自带水印。
电商商品图、截图素材、内容创作图片、AIGC 预处理图像……
如果不处理,产品体验会直接下降。
这时候就需要接入 图片去水印 API。
本文从开发者角度讲清楚:
- 图片去水印 API 是什么?
- 应用场景有哪些?
- 选型要看什么?
一、什么是图片去水印 API?
本质是:
通过 HTTP 接口提交图片,返回去除水印后的结果。
常见方式:
- 上传图片文件
- 或提交图片 URL
- 返回处理后的图片地址
二、典型应用场景
- 电商商品图处理
- 内容平台素材清洗
- 图像处理 SaaS
- 批量图片标准化
- AIGC 图像预处理
三、选型 7 大标准
- 去水印是否自然
- 是否自动识别水印区域
- 是否支持并发
- 响应时间
- 价格是否划算
- 接口设计是否清晰
- 是否支持在线测试
能在线测试效果,再决定是否接 API,非常关键。
四、实践方案
我测试使用的是「石榴智能图片去水印」:
- 提供在线图片去水印工具
- 同时提供 REST API
- 同时提供自动识别水印区域、手动选中水印区域
- 支持开发者接入
体验地址: www.shiliuai.com/inpaint/
五、API 示例
python代码接入
`# -- coding: utf-8 -- import requests import base64 import cv2 import json import numpy as np
api_key = '******' # 你的API KEY image_path = '...' # 图片路径
""" 用 image_base64 请求 """ with open(image_path, 'rb') as fp: image_base64 = base64.b64encode(fp.read()).decode('utf8')
url = 'api/auto_inpaint/v1' headers = {'APIKEY': api_key, "Content-Type": "application/json"} data = { "image_base64": image_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} or 失败:{'code': error_code, 'msg': error_msg, 'msg_cn': 错误信息} """ image_id = response['image_id'] result_base64 = response['result_base64'] file_bytes = base64.b64decode(result_base64) f = open('result.jpg', 'wb') f.write(file_bytes) f.close()
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 请求 """ data = { "image_id": image_id }
response = requests.post(url=url, headers=headers, json=data)`
php代码接入
`method = "POST"; header = array(); array_push(apikey); array_push($header, "Content-Type:application/json");
handle = fopen(image = fread(image_path)); fclose(image_base64 = base64_encode($image);
image_base64 ); data);
curl, CURLOPT_CUSTOMREQUEST, curl, CURLOPT_URL, curl, CURLOPT_HTTPHEADER, curl, CURLOPT_POSTFIELDS, curl, CURLOPT_RETURNTRANSFER, true); curl_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl); var_dump($response);`
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 filePath = "..."; // 图片路径 string url = "api/auto_inpaint/v1";
// 将图片编码为Base64
string photoBase64;
using (var imageStream = File.OpenRead(filePath))
{
byte[] imageBytes = new byte[imageStream.Length];
await imageStream.ReadAsync(imageBytes, 0, (int)imageStream.Length);
photoBase64 = Convert.ToBase64String(imageBytes);
}
// 构造请求数据
var requestData = new
{
image_base64 = photoBase64
};
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}");
}
}
}
}`
六、总结
对大多数团队来说:
接入成熟图片去水印 API,比自建模型更现实。
后续我会分别写:
- 批量图片去水印怎么做
- Python 接入示例
- 电商场景架构设计