赞
踩
目录
- cv2是基于OpenCV的图像处理库,可以对图像进行腐蚀,膨胀等操作;
- Numpy这是一个强大的处理矩阵和维度运算的库。
- pip install opencv-python
- pip install numpy
cv2的三个基本函数:使用cv2.imread()
,cv2.imshow()
,cv2.imwrite()
分别可以读取、显示和保存图像。
- import cv2
- import numpy as np
-
-
- def remove_watermark(image_path, output_path):
- # 读取图像, image_path='test.png'
- image = cv2.imread(image_path)
-
- cv2.imshow('test.png', image)
- cv2.waitKey(0)
- cv2.imwrite('test_2.png', image)
为了能够图片上点击水印的位置,并获取该水印的定位和大小,我们需要一个图形用户界面(GUI)库来与用户进行交互。Python中常用的GUI库有
tkinter
、PyQt
、wxPython
等。下面将使用tkinter
库来演示如何实现这个功能。
- import tkinter as tk
- from tkinter import messagebox
- from PIL import Image, ImageTk
-
- pos = []
-
-
- 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
- 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("Instructi

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。