针对水印位置不固定的情况批量去除水印,可以采用以下分步解决方案:
- Python自动化脚本(技术用户)
```python
import cv2
import numpy as np
def remove_watermark(image_path, output_path):
读取图像
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
水印检测(示例:检测底部10%区域)watermark_region = gray[height//10:height, :]
阈值处理
_, thresh = cv2.threshold(watermark_region, 127, 255, cv2.THRESH_BINARY)
橡皮擦擦除
kernel = np.ones((30, 30), np.uint8)
blurred = cv2.GaussianBlur(thresh, (25,25), 0)
eroded = cv2 erode(thresh, kernel, iterations=1)
mask = cv2.bitwise_not(eroded)
应用蒙版
result = cv2.bitwise_and(img, img, mask=mask)
cv2.imwrite(output_path, result)
批量处理
for file in os.listdir('input'):
if file.endswith('.jpg'):
remove_watermark(f'input/{file