美文网首页
python去除图片水印(部分可)

python去除图片水印(部分可)

作者: 是东东 | 来源:发表于2021-03-05 10:33 被阅读0次

可去除部分水印 有些完美去除 有些打码

# 必须安装pip install opencv-python 和 pillow

import cv2
import numpy as np
from PIL import Image


def drop_wartermark(path, newpath):
    img = cv2.imread(path, 1)
    # img.shape[:3] 则取彩色图片的高、宽、通道。
    hight, width, depth = img.shape[0:3]
    print(hight)
    print(width)
    # 裁剪水印坐标为[y0:y,x0:x1]
    cropped = img[int(hight * 0.9):hight, int(width * 0.7):width]
    # cropped = img[hight-49:hight, width-180:width]
    cv2.imwrite(newpath, cropped)
    # 将图片加载为内存对象 参一:完整路径;参二:flag:-1彩色,0灰色,1原有
    imgsy = cv2.imread(newpath, 1)
    
    # 图片二值化处理,把[200,200,200]~[255, 255, 255]以外的颜色变成0
    # 这个颜色区间就是水印周边的背景颜色
    thresh = cv2.inRange(imgsy, np.array([28, 28, 28]), np.array([54, 54, 54]))
    # #创建形状和尺寸的结构元素 创建水印蒙层
    kernel = np.ones((3, 3), np.uint8)
    # 对水印蒙层进行膨胀操作
    hi_mask = cv2.dilate(thresh, kernel, iterations=10)
    specular = cv2.inpaint(imgsy, hi_mask, 5, flags=cv2.INPAINT_TELEA)
    cv2.imwrite(newpath, specular)
    
    # 覆盖图片
    imgsy = Image.open(newpath)
    img = Image.open(path)
    img.paste(imgsy, (int(width * 0.7), int(hight * 0.9), width, hight))
    img.save(newpath)
    
    
drop_wartermark(path, newpath)

相关文章

网友评论

      本文标题:python去除图片水印(部分可)

      本文链接:https://www.haomeiwen.com/subject/vpdkqltx.html