import time import cv2 import pytesseract import win32gui import setting import win32_util from utils import ths_util, opencv_util, ocr_util def get_image(): scale = 1 always_save = False start_time = time.time() time_use_list = [] hwnd = ths_util.get_ths_main_content_hwnd_cached() time_use_list.append(("获取句柄", time.time() - start_time)) start_time = time.time() if not hwnd: raise Exception("看盘页面句柄未获取到") # 句柄截图 # 实际位置(左,上,右,下) rect = win32gui.GetWindowRect(hwnd) # 格式为:(上边距,宽度,高度,缩放倍数) rect_ = setting.get_ths_auto_code_rect() time_use_list.append(("获取配置参数", time.time() - start_time)) start_time = time.time() # 测试 # if len(rect_) > 3: # scale = rect_[3] width = int((rect[2] - rect[0]) * scale) left = width - rect_[1] img = win32_util.window_capture(hwnd, (left, rect_[0], width - 1, rect_[0] + rect_[2]), scale, always_save) time_use_list.append(("截图", time.time() - start_time)) start_time = time.time() if always_save: cv2.imwrite(f"test_clip_gray.png", opencv_util.gray_img(img)) clip_img, details = opencv_util.clip_ths_code_area(img) if always_save: cv2.imwrite(f"test_clip_gray_clip.png", clip_img) time_use_list.append(("分割", time.time() - start_time)) start_time = time.time() print(time_use_list) return clip_img if __name__ == "__main__": pytesseract.pytesseract.tesseract_cmd = r'D:\Program Files\Tesseract-OCR\tesseract.exe' img = get_image() for i in range(100): start_time = time.time() # text = pytesseract.image_to_string(img, config=config) text = ocr_util.recognize_code(img) print((time.time() - start_time) * 1000, text)