import cv2
import numpy as np
from skimage.restoration import denoise_nl_means, estimate_sigma
from skimage.restoration.inpaint import inpaint_biharmonic
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
pos = []
size = []
def get_click_positions(canvas, image, root):
"""获取用户点击的四个角位置"""
positions = []
click_count = 0
def on_click(event):
print("点击")
nonlocal click_count, positions
x = event.x
y = event.y
positions.append((x, y))
click_count += 1
if click_count == 4:
# 用户已点击四个角,关闭画布并返回位置
canvas.unbind('<Button-1>')
print(f"Watermark positions: {positions}")
# 计算水印大小和位置
# positions.sort(key=lambda p: p[0] + p[1]) # 根据点击顺序排序
top_left, top_right, bottom_right, bottom_left = positions
x1, y1 = top_left
x2, y2 = bottom_right
width = x2 - x1
height = y2 - y1
size.append((width, height))
print(f"Watermark position: ({x1}, {y1}), Size: ({width}x{height})")
# 关闭程序
root.destroy()
# canvas.bind('<Button-1>', on_click)
canvas.config(cursor="cross") # 设置鼠标样式为十字线
# 显示图片
photo = ImageTk.PhotoImage(image)
image_label = tk.Label(canvas, image=photo)
image_label.image = photo
image_label.place(x=0, y=0, relwidth=1, relheight=1)
image_label.bind('<Button-1>', on_click)
# canvas.pack(fill=tk.BOTH, expand=tk.YES)
canvas.pack()
messagebox.showinfo("Instructions", "Please click on the four corners of the watermark in sequence.")
# 运行tkinter事件循环直到用户点击四个角
root.mainloop()
return positions
def get_tk_window(image_path):
"""获取用户点击的窗口"""
# 加载图片
image = Image.open(image_path)
# 创建tkinter窗口
root = tk.Tk()
# root.withdraw() # 隐藏主窗口
# 获取点击位置
global pos
pos = get_click_positions(tk.Canvas(root, width=image.width, height=image.height), image, root)
# 如果没有点击四个角,positions将为空
if not pos:
messagebox.showerror("Error", "Not enough corner positions received.")
else:
print("Positions received:", pos)
def remove_watermark(image_path, output_path, watermark_position, inpaintRadius):
""" 去除水印 """
# 读取图像
image = cv2.imread(image_path)
if image is None:
print("无法读取图像")
return
# 定义水印的大小
watermark_width, watermark_height = watermark_position['size']
# 定义水印的左上角坐标
x, y = watermark_position['position']
# 创建一个掩码,标记出水印区域
mask = np.zeros_like(image)
mask[y:y + watermark_height, x:x + watermark_width] = 1
# 确保掩模是8位无符号整数类型
gray_mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
# 使用inpaint函数进行修复
dst = cv2.inpaint(image, gray_mask, inpaintRadius, cv2.INPAINT_TELEA)
# 保存修复后的图像
cv2.imwrite(output_path, dst)
# 水印的位置和大小
# watermark_position = {
# 'position': (386, 449), # 水印左上角的坐标 (x, y)
# 'size': (111, 46) # 水印的宽度和高度 (width, height)
# }
get_tk_window('./input.jpg')
# 水印的位置和大小
watermark_position = {
'position': pos[0], # 水印左上角的坐标 (x, y)
'size': size[0] # 水印的宽度和高度 (width, height)
}
# 调用函数去除并修复水印,
# 可调节inpaintRadius半径大小优化修复效果,值大整体效果较好,小局部较好
remove_watermark('input.jpg', 'output6.jpg', watermark_position, 300)
remove_watermark('output6.jpg', 'output7.jpg', watermark_position, 20)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Python编程-使用OpenCV和Numpy库实现图片去水印-代码 包含; 图像读取转化, 自定义点击选取水印位置, 去除水印, 图片修复 cv2是基于OpenCV的图像处理库,可以对图像进行腐蚀,膨胀等操作; Numpy这是一个强大的处理矩阵和维度运算的库。 文章地址:https://blog.csdn.net/qq_44695727/article/details/136146895 水印覆盖,图片修复效果优化 !!! 请注意,去水印可能涉及版权问题,确保你只在你有权处理的图像上尝试这些方法,并且不要用于非法目的
资源推荐
资源详情
资源评论
收起资源包目录
watermarkApp.rar (1个子文件)
watermarkApp.py 4KB
共 1 条
- 1
资源评论
瑶山
- 粉丝: 3w+
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功