只需要使用鼠标画矩形框,然后电脑会依次点击1,2,3,三个红色的框,速度很快。事半功倍。
原理就是在桌面上找图然后点击。
视频演示:
代码部分:
注意:代码中有三个图片,分别是
ok.png
next.png
create_box.png
只需要使用qq截图功能将以上3个图片换成你自己的就可以了。
import aircv as ac
from PIL import ImageGrab
import win32api
import win32con
from ctypes import *
import cv2 as cv
import pynput
import time
import pyautogui
import numpy as np
def draw_circle(pos, circle_radius, color, line_width, src_path):
imsrc = ac.imread(src_path)
cv.circle(imsrc, pos, circle_radius, color, line_width)
cv.imshow('objDetect', imsrc)
cv.waitKey(0)
cv.destroyAllWindows()
def find_image_cv(obj_path, src_path):
# basefolder = os.path.abspath('.') + "\\source\\"
#ImageGrab.grab().save(src_path)
# source = cv.imread(src_path)
source=src_path
template = cv.imread(obj_path)
result = cv.matchTemplate(source, template, cv.TM_CCOEFF_NORMED)
# print(result)
pos_start = cv.minMaxLoc(result)[3]
test = cv.minMaxLoc(result)
# print(test)
# print(pos_start)
x = int(pos_start[0]) + int(template.shape[1] / 2)
y = int(pos_start[1]) + int(template.shape[0] / 2)
similarity = cv.minMaxLoc(result)[1]
if similarity < 0.85:
return (-1, -1)
else:
#print("pass")
return [(x, y),(x, y)]
def double_click(obj_path, src_path):
p, q = find_image_cv(obj_path, src_path)
x, y = p
windll.user32.SetCursorPos(x, y)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def desk():
img = pyautogui.screenshot()
img = np.array(img)
# print(img.shape)
img = cv.cvtColor(img, cv.COLOR_RGB2BGR)
return img
with pynput.mouse.Events() as event:
for i in event:
# 迭代用法。
if isinstance(i, pynput.mouse.Events.Move):
# 鼠标移动事件。
# print(i.x, i.y)
# 不要直接打印`i`,模块这里有问题,会报错。
pass
elif isinstance(i, pynput.mouse.Events.Click):
# 鼠标点击事件。
# print(i.button, i.pressed)
# print(type(i.button),type(i.pressed))
# print(str(i.button))
if str(i.button)=='Button.left' and i.pressed==False:
# 寻找图片并单击
# 单击ok
while True:
try:
double_click(obj_path='ok.png', src_path=desk())
time.sleep(0.3)
break
except:
continue
# 单击next
while True:
try:
double_click(obj_path='next.png', src_path=desk())
# time.sleep(0.5)
break
except:
continue
# 单击create_box
while True:
try:
double_click(obj_path='create_box.png', src_path=desk())
# time.sleep(0.5)
break
except:
continue
pass
# 这个i.button就是上文所说的“鼠标按键”中的一个,用is语句判断即可。
elif isinstance(i, pynput.mouse.Events.Scroll):
# 鼠标滚轮。
# cv.imshow('a12',img)
# cv.waitKey(0)
pass
# i = event.get(1)