python利用opencv去除图片logo_python +opencv去除水印

1.工具

python 3.7   opencv4.1

2.思路

1)获取图片水印的三原色,利用opencv 的inRange 二值化处理 (获取水印三原色范围)

2)opencv  的dilate  进行膨胀处理 (将水印三原色覆盖掉)

3)opencv   的inpaint 进行修复

3.代码

# 图片修复

import cv2

import numpyas np

import os

import time

def cv_imread(filePath):##  处理cv2 读取中文文件问题  ##

cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)

cv_img=cv2.cvtColor(cv_img,cv2.IMREAD_COLOR)

return cv_img

def test(path,file):

img = cv_imread(path)

hight, width, depth = img.shape[0:3]

# 图片二值化处理,把[240, 240, 240]~[255, 255, 255]以外的颜色变成0

thresh = cv2.inRange(img, np.array([240, 240, 240]), np.array([255, 255, 255]))

# 创建形状和尺寸的结构元素

kernel = np.ones((3, 3), np.uint8)

# 扩张待修复区域

hi_mask = cv2.dilate(thresh, kernel, iterations=1)

specular = cv2.inpaint(img, hi_mask, 5, flags=cv2.INPAINT_TELEA)

cv2.namedWindow("Image", 0)

cv2.resizeWindow("Image", int(width /2), int(hight /2))

cv2.imshow("Image", img)

cv2.namedWindow("newImage", 0)

cv2.resizeWindow("newImage", int(width /2), int(hight /2))

cv2.imshow("newImage", specular)

#cv2.imwrite("C:/Users/xxx/Desktop/aa/"+file,specular)

cv2.imencode('.jpg', specular)[1].tofile("C:/Users/xxx/Desktop/aa/"+file)####将去水印图片放入指定文件夹#####

#cv2.waitKey(0)

#cv2.destroyAllWindows()

return

path ="C:/Users/xxxx/Desktop/dd/"

for root,dirs,filein os.walk(path):###循环获取文件夹下 文件绝对路径 ##

for sin file:

_path = path + s

print(_path)

test(_path,s)

4.效果

5 缺点

这个方法是用的颜色进行判断 如果图片颜色和 水印颜色相近 会照成图片污染

如果水印颜色和取值范围颜色不一致会有残留

水印过小效果不明显

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值