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

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

# 必须安装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)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值