-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
130 lines (109 loc) · 4.69 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#若该键盘鼠标的操作被检测到了,可以使用pip install pywinio模块,制作驱动级别的键鼠模拟
from ctypes import windll, byref, c_ubyte
from ctypes.wintypes import RECT, HWND
import numpy as np
import pyautogui
import time
from win32com.client import Dispatch
def getmouse(): #得到鼠标当前位置
x,y=pyautogui.position()
return x,y
def moveto(x,y,movetime=0): #鼠标移动 x,y_移动位置,movetime_移动时间
pyautogui.moveTo(x,y,duration=movetime)
def click_l(): #当前位置左键单击
x,y=pyautogui.position()
pyautogui.click(x,y,button="left")
def click_r(): #当前位置右键单击
x,y=pyautogui.position()
pyautogui.click(x,y,button="right")
def dbclick_l(): #左键双击
pyautogui.doubleClick(button="left")
def dbclick_r(): #右键双击
pyautogui.doubleClick(button="right")
def dbclick_m(): #中键双击
pyautogui.doubleClick(button="middle")
def mousedown(): #按下
pyautogui.mouseDown()
def mouseup(): #释放
pyautogui.mouseUp()
def scroll(sizes=0, x=None, y=None):#滑轮滑动 sizes_int类型,x,y_在x,y滑动位置
pyautogui.scroll(sizes, x , y)
# import mouse as mouse
GetDC = windll.user32.GetDC
CreateCompatibleDC = windll.gdi32.CreateCompatibleDC
GetClientRect = windll.user32.GetClientRect
CreateCompatibleBitmap = windll.gdi32.CreateCompatibleBitmap
SelectObject = windll.gdi32.SelectObject
BitBlt = windll.gdi32.BitBlt
SRCCOPY = 0x00CC0020
GetBitmapBits = windll.gdi32.GetBitmapBits
DeleteObject = windll.gdi32.DeleteObject
ReleaseDC = windll.user32.ReleaseDC
# 防止UI放大导致截图不完整
#windll.user32.SetProcessDPIAware()
def capture(handle: HWND):
"""窗口客户区截图
Args:
handle (HWND): 要截图的窗口句柄
Returns:
numpy.ndarray: 截图数据
"""
# 获取窗口客户区的大小
r = RECT()
GetClientRect(handle, byref(r))
width, height = r.right, r.bottom
# 开始截图
dc = GetDC(handle)
cdc = CreateCompatibleDC(dc)
bitmap = CreateCompatibleBitmap(dc, width, height)
SelectObject(cdc, bitmap)
BitBlt(cdc, 0, 0, width, height, dc, 0, 0, SRCCOPY)
# 截图是BGRA排列,因此总元素个数需要乘以4
total_bytes = width*height*4
buffer = bytearray(total_bytes)
byte_array = c_ubyte*total_bytes
GetBitmapBits(bitmap, total_bytes, byte_array.from_buffer(buffer))
DeleteObject(bitmap)
DeleteObject(cdc)
ReleaseDC(handle, dc)
# 返回截图数据为numpy.ndarray
return np.frombuffer(buffer, dtype=np.uint8).reshape(height, width, 4)
if __name__ == "__main__":
import cv2
op=Dispatch("op.opsoft")
handle = windll.user32.FindWindowW(None, "剑破长空 - 002.003.00003 - 一服 - ☆小小锅巴仔 - 1801 - ")
# 截图时要保证游戏窗口的客户区大小是1334×750
image = capture(handle)
# 转为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
# 读取图片,并保留Alpha通道,若要排除背景干扰,需要将源对比图背景设置成透明的,即Alpha通道置为0.
template = cv2.imread('zeitou1.png', cv2.IMREAD_UNCHANGED)
# 转为灰度图
template_gray = cv2.cvtColor(template, cv2.COLOR_BGRA2GRAY)
# 取出Alpha通道
alpha = template[:,:,3]
# 模板匹配,将alpha作为mask,TM_CCORR_NORMED方法的计算结果范围为[0, 1],越接近1越匹配 TM_CCOEFF_NORMED
result = cv2.matchTemplate(gray, template_gray, cv2.TM_CCOEFF_NORMED,mask=alpha)
# TM_SQDIFF 平方差匹配法:该方法采用平方差来进行匹配;最好的匹配值为0;匹配越差,匹配值越大。
# TM_CCORR 相关匹配法:该方法采用乘法操作;数值越大表明匹配程度越好。
# TM_CCOEFF 相关系数匹配法:1表示完美的匹配;-1表示最差的匹配。
# TM_SQDIFF_NORMED 归一化平方差匹配法
# TM_CCORR_NORMED 归一化相关匹配法
# TM_CCOEFF_NORMED 归一化相关系数匹配法
# result = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
print(cv2.minMaxLoc(result))
# 获取结果中最大值和最小值以及他们的坐标
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# 匹配最大值的坐标,左上角
top_left = max_loc
print ("top left: ", top_left)
# 模板的高度和宽度
h, w = template.shape[:2]
# 右下角的坐标
bottom_right = top_left[0] + w, top_left[1] + h
# 在窗口截图中匹配位置画红色方框,在image上画出左上角为top_left,右下角为bottom_right的坐标的矩形
if max_val > 0.8:
op.MoveTo(1920/2,1080/2)
cv2.rectangle(image, top_left, bottom_right, (0,0,255), 2)
cv2.imshow('Match Template', image)
cv2.waitKey()