图片修复 API 接入实战:网站如何自动去除图片水印(Python / PHP / C# 示例)
在很多网站或系统中,经常需要对图片进行处理,例如:
-
清理图片水印
-
修复老照片
-
去除图片上的文字或杂物
-
优化图片视觉效果
如果完全自己开发图像算法,成本会非常高。因此现在很多开发者会选择 调用图像处理 API 来快速实现。
这篇文章就介绍 如何通过 API 实现自动图片修复 / 去水印功能。
并提供 Python / PHP / C# 示例代码,方便快速接入。
一、图片去水印的常见实现方式
目前主流实现方式主要有三种:
1 手动 PS 处理
优点:
-
处理效果最好
缺点:
-
无法自动化
-
不适合网站批量处理
2 使用 AI 图像修复算法
利用 AI 自动识别并修复图片中的水印区域,例如:
-
文字水印
-
Logo 水印
-
图片瑕疵
这种方式可以 自动化处理图片,非常适合网站或 SaaS 工具。
3 调用图像处理 API
如果你不想自己训练模型,可以直接调用图像处理 API。
例如:
-
自动图片修复
-
图片去水印
-
图片增强
-
图片变高清
在线体验示例:
用户上传图片即可自动处理。
二、API 接入流程
一般图片处理 API 接入流程非常简单:
1️⃣ 注册获取 API Key
2️⃣ 上传图片
3️⃣ 调用接口处理
4️⃣ 获取处理结果
调用流程如下:
用户上传图片
↓
服务器接收图片
↓
调用图片修复 API
↓
返回处理后的图片
详细接入文档可以参考:https://www.shiliuai.com/api/zidongqushuiyin

三、Python 调用示例
下面是一个简单的 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 = 'https://api.shiliuai.com/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)
返回示例:
{
"code": 0,
"msg": "OK",
"msg_cn": "成功",
"result_base64": "/9j/4AAQSkZJRgABAQAAAQABAAD...",
"image_id": "b6a0f7d0b2f54d0ea3..."
}
// 失败示例
{
"code": 4,
"msg": "Invalid parameter: image_base64 or image_id is required",
"msg_cn": "参数错误:image_base64 或 image_id 必须填写其中之一"
}
四、PHP 接入示例
$url = "https://api.shiliuai.com/api/auto_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);
$data = array(
"image_base64"=> $image_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);
五、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 = "https://api.shiliuai.com/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 在很多场景中都可以使用,例如:
1 电商图片处理
自动清理图片水印,统一商品图片风格。
2 图片工具网站
例如:
-
图片修复
-
图片增强
-
去水印
-
抠图
很多在线图片工具都是通过 API 实现的。
3 内容平台
用户上传图片时,自动处理图片中的瑕疵或标记。
七、在线工具示例
如果你想先体验效果,也可以直接使用在线工具。
示例:
图片去水印在线工具
https://www.shiliuai.com/auto_inpaint/

上传图片即可自动处理。
八、总结
通过调用 图像修复 API,开发者可以非常快速地实现:
-
自动图片去水印
-
图片修复
-
图片增强
-
图片清理
相比自己开发算法:
-
接入成本更低
-
开发周期更短
-
可以快速上线功能
如果你的项目中需要图片处理能力,API 接入是一个非常高效的方案。
如果大家在 OCR识别、图片处理 API 接入方面有问题,也欢迎一起交流。
后面也会继续分享:
-
OCR 文档识别
-
图片增强 API
-
抠图 API 接入
-
证件照制作 API 实战
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)