这里先感谢一下 @杰森还有头发 给我的启发
前提条件:
①现在Tesseract-OCR并且配置好安装路径,然后安装好pytesseract库和pyautogui库
②根据以下代码找出你所需要的比大小的数字的位置数据
import pyautogui import time def find_area(): print("请将鼠标移动到可能包含第一个数字的区域的左上角,然后按下回车键。") input() top_left = pyautogui.position() print(f"左上角坐标:{top_left}") print("请将鼠标移动到可能包含第一个数字的区域的右下角,然后按下回车键。") input() bottom_right = pyautogui.position() print(f"右下角坐标:{bottom_right}") width = bottom_right[0] - top_left[0] height = bottom_right[1] - top_left[1] return top_left[0], top_left[1], width, height print("开始确定第一个数字的截图区域。") first_area = find_area() print("开始确定第二个数字的截图区域。") second_area = find_area() print(f"第一个数字截图区域:{first_area}") print(f"第二个数字截图区域:{second_area}")
全自动答题代码如下
import pytesseract import pyautogui import time pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' region1 = (1080, 400, 264, 158) # 左边数字位置 region2 = (1479, 401, 419, 191) # 右边数字位置 write_region = (239, 1065, 2520, 368) # 绘制符号位置 def draw_larger_less_than(): start_x, start_y = write_region[0] + 10, write_region[1] - 10 pyautogui.moveTo(start_x, start_y) pyautogui.mouseDown() new_x, new_y = start_x - 25, start_y + 25 pyautogui.moveTo(new_x, new_y, duration=0.16) new_x, new_y = new_x + 25, new_y - 10 pyautogui.moveTo(new_x, new_y, duration=0.16) pyautogui.mouseUp() def draw_larger_greater_than(): start_x, start_y = write_region[0] + 30, write_region[1] - 10 pyautogui.moveTo(start_x, start_y) pyautogui.mouseDown() new_x, new_y = start_x + 25, start_y + 25 pyautogui.moveTo(new_x, new_y, duration=0.16) new_x, new_y = new_x - 25, new_y - 10 pyautogui.moveTo(new_x, new_y, duration=0.16) pyautogui.mouseUp() def draw_equal(): start_x, start_y = write_region[0] + 20, write_region[1] - 10 # 绘制两条平行的横线表示等号 for offset in [0, 20]: # 使用偏移量绘制第二条线 pyautogui.moveTo(start_x, start_y + offset) pyautogui.mouseDown() pyautogui.moveTo(start_x + 40, start_y + offset, duration=0.2) # 绘制宽度为40的线 pyautogui.mouseUp() # 松开鼠标,完成绘制 while True: screenshot1 = pyautogui.screenshot(region=region1) screenshot1.save('number1.png') number1_str = pytesseract.image_to_string('number1.png', config='--psm 6 -c tessedit_char_whitelist=0123456789') filtered_number1 = ''.join(filter(str.isdigit, number1_str)) if filtered_number1: number1 = int(filtered_number1) print(f"第一个数字:{number1}") else: number1 = None screenshot2 = pyautogui.screenshot(region=region2) screenshot2.save('number2.png') number2_str = pytesseract.image_to_string('number2.png', config='--psm 6 -c tessedit_char_whitelist=0123456789') filtered_number2 = ''.join(filter(str.isdigit, number2_str)) if filtered_number2: number2 = int(filtered_number2) print(f"第二个数字:{number2}") else: number2 = None if number1 is not None and number2 is not None: print(f"比较数字:{number1} 和 {number2}") if number1 > number2: draw_larger_greater_than() print("大于") elif number1 < number2: draw_larger_less_than() print("小于") else: draw_equal() print("等于") # 输出等于的信息 else: print("无法正确识别数字。") time.sleep(0.000001)