Python处理图片水印(亲测有效)

Python去除水印

 

直接上代码

import cv2
import numpy as np
from PIL import Image
import os
dir = os.getcwd()
path = "2.jpg"
newPath = "new2.jpg"
img = cv2.imread(path, 1)
hight, width, depth = img.shape[0:3]
# 截取
cropped = img[int(hight * 0.8):hight, int(width * 0.7):width]  # 裁剪坐标为[y0:y1, x0:x1]
cv2.imwrite(newPath, cropped)
imgSY = cv2.imread(newPath, 1)
# 图片二值化处理,把[200,200,200]-[250,250,250]以外的颜色变成0
thresh = cv2.inRange(imgSY, np.array([200, 200, 200]), np.array([250, 250, 250]))
# 创建形状和尺寸的结构元素
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.8), width, hight))
img.save(newPath)

前后对比结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值