图片去水印的API或解决方案可以根据需求选择不同的技术路线,以下是几种常见方式及示例:
一、开源技术方案(适合开发者自研)
- Pillow(Python)
```python
from PIL import Image, ImageChops
def remove_watermark(input_path, output_path):
img = Image.open(input_path)
alpha = img.getchannel(3) 获取Alpha通道(适用于半透明水印)
img = ImageChops.darker(img, alpha) 通过Alpha通道减淡水印
img.save(output_path)
```
- OpenCV(C++/Python)
```python
import cv2
import numpy as np
def remove_watermark_opencv(input_path):
img = cv2.imread(input_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((3,3), np.uint8)
dilation = cv2.dilate(thresh, kernel, iterations=1)
img = cv2.bitwise_and(img, img, mask=dilation)
return img
```
二、第三方API服务(无需技术实现)
- Clarity AI
- 支持自动检测并移除复杂水印
- API文档:https://clarity.ai/docs
示例请求:
POST /v1/images/remove-watermark
Body: {
"image_url": "https://example.com/image.jpg"