From 00a984ac6146c600b71322c637b9e24681b11175 Mon Sep 17 00:00:00 2001 From: awinx Date: Wed, 15 Jan 2025 21:54:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E6=96=87=E5=AD=97?= =?UTF-8?q?=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 7 ++----- pinPicture.py | 33 ++++++++++++++++++++++----------- requirements.txt | Bin 408 -> 468 bytes 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 4131c69..6e6088b 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import pinPicture -from PIL import Image import tkinter as tk +import numpy as np import pyautogui import keyboard @@ -14,9 +14,6 @@ def capture_screen(): root.mainloop() - -# 监听特定快捷键,这里假设是Ctrl+Alt+S keyboard.add_hotkey('F7', capture_screen) -# 保持程序运行,持续监听快捷键 -keyboard.wait() \ No newline at end of file +keyboard.wait() diff --git a/pinPicture.py b/pinPicture.py index 9cf2ab0..dc597da 100644 --- a/pinPicture.py +++ b/pinPicture.py @@ -2,13 +2,15 @@ import io import math import tkinter as tk import win32clipboard -from PIL import Image, ImageTk, ImageChops, ImageDraw, ImageGrab -import threading +from PIL import Image, ImageTk, ImageDraw +import numpy as np +import easyocr class PinPicture(tk.Frame): def __init__(self, image, master=None): super().__init__(master) + self.detectBtn = None self.pinBtn = None self.lineBtn = None self.arrowBtn = None @@ -34,9 +36,10 @@ class PinPicture(tk.Frame): self.drawing = False self.drawMode = False self.color = "red" - self.image = image if image.mode != "RGBA": - image = image.convert("RGBA") + self.image = image.convert("RGBA") + else: + self.image = image self.create_window() self.penWidth = 3 self.arrowSize = 10 @@ -44,7 +47,6 @@ class PinPicture(tk.Frame): # 创建窗口 def create_window(self): self.master.overrideredirect(True) - self.master.attributes('-transparentcolor', 'white') self.master.config(bg='white') self.master.attributes('-topmost', True) self.canvas = tk.Canvas(self.master, height=self.image.height, width=self.image.width) @@ -53,6 +55,8 @@ class PinPicture(tk.Frame): self.arrowBtn = tk.Button(text='箭头', command=self.set_arrow) self.lineBtn = tk.Button(text='直线', command=self.set_line) self.pinBtn = tk.Button(text='确定', command=self.pin_mode_event) + self.drawModeBtn = tk.Button(text="编辑", command=self.draw_mode_event) + self.detectBtn = tk.Button(text="识别", command=self.detect) self.master.bind("", self.close) self.master.bind("", self.enter_canvas) @@ -69,7 +73,7 @@ class PinPicture(tk.Frame): def on_save(self, event): output = io.BytesIO() self.image.convert("RGB").save(output, "BMP") - data = output.getvalue()[14:] # BMP 文件头前14字节 + data = output.getvalue()[14:] # BMP 文件头前14bit output.close() win32clipboard.OpenClipboard() try: @@ -112,16 +116,15 @@ class PinPicture(tk.Frame): # 鼠标进入 def enter_canvas(self, event): self.canvas.focus_set() - if self.drawModeBtn is None and not self.drawMode: - self.drawModeBtn = tk.Button(text="编辑", command=self.draw_mode_event) - self.drawModeBtn.pack(side=tk.TOP, expand=True) - elif not self.drawMode: + if not self.drawMode: self.drawModeBtn.pack(side=tk.TOP, expand=True) + self.detectBtn.pack(side=tk.TOP, expand=True) # 鼠标离开 def leave(self, event): if self.drawModeBtn is not None and not self.drawMode: self.drawModeBtn.pack_forget() + self.detectBtn.pack_forget() # 进入编辑模式 def draw_mode_event(self): @@ -262,7 +265,7 @@ class PinPicture(tk.Frame): cosa = (x2 - x1) / l sina = (y2 - y1) / l d = self.arrowSize * (sina + cosa) - if (sina < cosa): + if sina < cosa: xa = x2 - d ya = y2 + math.sqrt(2 * self.arrowSize * self.arrowSize - d * d) xb = x2 - math.sqrt(2 * self.arrowSize * self.arrowSize - d * d) @@ -291,3 +294,11 @@ class PinPicture(tk.Frame): self.image = Image.alpha_composite(self.image, self.pad) self.pad = Image.new('RGBA', self.image.size) self.drawPad = ImageDraw.Draw(self.pad) + + def detect(self): + reader = easyocr.Reader(['ch_sim', 'en']) + result = reader.readtext(np.array(self.image), detail=0) + text = '' + for str in result: + text += str + print (text) diff --git a/requirements.txt b/requirements.txt index 50a268b45b8616857e787f7d5858aee0880d95f9..768dc520e78cd954d669903d4ba3c12f608b370d 100644 GIT binary patch delta 68 zcmbQie1&<#3`QGX1}=tFhD3&9hDwHfhGd2!hB^jY215ot26G@bVgSkKF_bdoG86z+ MfaHw8;(9