使用opencv去除水印

实现去除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

参考
简单!Python+OpenCV三步去除水印

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值