实现去除pdf水印
pdf水印在每一页的大小位置相同
水印颜色与正常字体颜色稍微浅些
思路
pdf转图片
制作水印蒙版图
去除水印
根据水印蒙版图找出原图的水印位置
只对水印部分处理
合成图片
图片转pdf
所使用的工具和库
Adobe Acrobat 9 Pro
Pycharm
python
opencv
PIL
numpy
核心代码
def levelsDeal(img, img2):
"""
img: 原图
img2:水印蒙版图
"""
thresh = cv2.inRange(img2, np.array([40]), np.array([200]))
scan = np.ones((5, 5), np.uint8)
cor = cv2.dilate(thresh, scan, iterations=1)
img_array = np.array(img, dtype=int)
h1, w1, _ = img_array.shape
h2, w2 = cor.shape
h = min(h1,h2)
w = min(w1, w2)
print(cor.shape)
img_array = img_array[:h,:w,:1].reshape(h, w)
cor = cor[:h,:w]
# 找出有水印的地方,有水印的为1
remove_watermark = cor/255*img_array
# 利用色差去除水印,根据实际情况自己修改
#img_array = np.minimum(img_array, 100).astype(np.uint8)
remove_watermark = np.clip(remove_watermark*4-140, 0, 255).astype(np.uint8)
# 原图水印部分去除
img_array[cor > 0] = 0
# 原图与去水印部分叠加
img_array = np.clip(img_array+remove_watermark, 0, 255).astype(np.uint8)
return img_array