This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathHookWindow.py
244 lines (179 loc) · 7.37 KB
/
HookWindow.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import numpy as np
import cv2
from PIL import Image, ImageOps
from conf.conf_manager import ConfManager
data = ConfManager.get('conf.json')
if data['platform'] == "windows":
from ctypes import windll
import win32ui
import win32gui
hwnd = data['hwnd']
elif data['platform'] == "linux":
import os
from core.LinuxClient import Execute, FindAnotherWindow, FindWindow
TEMPLATES_PATH = os.path.dirname(__file__) + "/../images/"
AnotherWindow = FindAnotherWindow()
Window = FindWindow()
class Hooker:
if data['platform'] == 'windows':
def __init__(self):
self.hwnd = hwnd
self.win32gui = win32gui
self.win32ui = win32ui
self.dll = windll.user32
self.hwndDC = self.win32gui.GetWindowDC(self.hwnd)
self.mfcDC = self.win32ui.CreateDCFromHandle(self.hwndDC)
self.saveDC = self.mfcDC.CreateCompatibleDC()
self.saveBitMap = self.win32ui.CreateBitmap()
self.left, self.top, self.right, self.bot = self.win32gui.GetClientRect(self.hwnd)
self.left = self.left + 8
self.top = self.top + 8
self.right = self.right + 8
self.bot = self.bot + 8
self.w = self.right - self.left
self.h = self.bot - self.top
self.saveBitMap.CreateCompatibleBitmap(self.mfcDC, self.w, self.h)
self.saveDC.SelectObject(self.saveBitMap)
self.result = self.dll.PrintWindow(self.hwnd, self.saveDC.GetSafeHdc(), 1)
self.bmpinfo = self.saveBitMap.GetInfo()
self.bmpstr = self.saveBitMap.GetBitmapBits(True)
self.TakedImage = Image.frombuffer(
'RGB',
(self.bmpinfo['bmWidth'], self.bmpinfo['bmHeight']),
self.bmpstr, 'raw', 'BGRX', 0, 1)
self.win32gui.DeleteObject(self.saveBitMap.GetHandle())
self.saveDC.DeleteDC()
self.mfcDC.DeleteDC()
self.win32gui.ReleaseDC(self.hwnd, self.hwndDC)
elif data['platform'] == 'linux':
def __init__(self, FileName="images/tmp_screen.png"):
Execute(["scrot", "-u", FileName])
self.TakedImage = cv2.imread(FileName)
if self.TakedImage is not None:
self.result = 1
def HookWindow(self):
if self.result == 1:
return self.TakedImage
else:
self.win32gui.SetForegroundWindow(self.hwnd)
self.TakedImage = Hooker().HookWindow()
if self.result == 1:
return self.TakedImage
else:
return print('Debugged From HookWindow')
'''
Here It Call The Hook Class, Passing The Region For Crop,
Returning The Img Cropped.
'''
def TakeImage(Region=None):
Except = True
while Except:
try:
TakedImage = Hooker().HookWindow()
if Region is not None:
TakedImage = TakedImage.crop(
(Region[0], Region[1], Region[0] + (Region[2] - Region[0]), Region[1] + (Region[3] - Region[1])))
return TakedImage
else:
return TakedImage
except Exception as Ex:
# print("Debugged From TakeImage: ", Ex)
Except = True
pass
'''
In LocateImage, It Compare With Another Image, If The Two Have Any
Similarity To Each Other.
If Have Similarity, He Return The X And Y Position.
Examples:
1°: LocateImage('images/TibiaSettings/BattleList.png')
2°: LocateImage('images/TibiaSettings/BattleList.png', (1234, 435, 1280, 460)) == He Locate The Image On Passed Folder,
In X: 1234 Up Until 1280 And Y: 435 Up Until 460.
'''
def LocateImage(image, Region=None, Precision=0.8):
TakedImage = TakeImage(Region)
img_rgb = np.array(TakedImage)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(image, 0)
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
min_val, LocatedPrecision, min_loc, Position = cv2.minMaxLoc(res)
if LocatedPrecision > Precision:
return Position[0], Position[1]
return 0, 0
'''
In LocateCenterImage, It Compare With Another Image, If The Two Have Any
Similarity To Each Other.
If Have Similarity, He Return The X And Y Position More Half Of Size The Image
For Return The Position Of Center Of Image...
'''
def LocateCenterImage(image, Region=None, Precision=0.8):
TakedImage = TakeImage(Region)
img_rgb = np.array(TakedImage)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(image, 0)
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
min_val, LocatedPrecision, min_loc, Position = cv2.minMaxLoc(res)
if LocatedPrecision > Precision:
needleWidth, needleHeight = GetImageSize(image)
if needleWidth:
return Position[0] + int(needleWidth / 2), Position[1] + int(needleHeight / 2)
else:
print('Debugged From LocateCenterImage')
return 0, 0
'''
Here, It Search For All Images Passed From Argument
And It Return The Number Of Images Found.
Examples:
LocateAllImages('images/Foods/Cheese.png'), He Search For All Cheeses On Your Screen,
And Will Return This Number.
'''
def LocateAllImages(image, Region=None, Precision=0.8):
TakedImage = TakeImage(Region)
TakedImage = np.array(TakedImage)
img_gray = cv2.cvtColor(TakedImage, cv2.COLOR_BGR2GRAY)
template = cv2.imread(image, 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= Precision)
Number = 0
for pt in zip(*loc[::-1]):
Number = Number + 1
return Number
def LocateBoolRGBImage(image, Region=None, Precision=0.9):
SaveImage('images/MonstersAttack/VerifyAttacking.png', Region)
img_rgb = cv2.imread('images/MonstersAttack/VerifyAttacking.png', 0)
template = cv2.imread(image, 0)
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
min_val, LocatedPrecision, min_loc, Position = cv2.minMaxLoc(res)
if LocatedPrecision > Precision:
return True
return False
'''
In PixelMatchesColor, It Compare The RGB Color Of The Pixel Nedded,
But This Dont Work 100% >.< Hahahaha.
Example:
PixelMatchesColor(657, 432, (255, 255, 255)), He Verify If Pixel X: 657 And Y: 432, Is Black.
'''
def PixelMatchesColor(X, Y, expectedRGBColor):
TakedImage = TakeImage(Region=(X, Y, X + 1, Y + 1))
rgb = TakedImage.getpixel((0, 0))
if rgb == expectedRGBColor:
return True
else:
return False
'''
When Called, It Save The Image On Folder And Name Passed Per Argument, Examples:
1°: SaveImage('TakedImage.png') == Save The Image In Current Directory,
2°: SaveImage('images/Foods/Cheese.png', (1234, 435, 1280, 460)) == Save The Image On Passed Folder,
And Crop Then In X: 1234 Up Until 1280 And Y: 435 Up Until 460.
'''
def SaveImage(Name, Region=None):
TakedImage = TakeImage(Region)
return TakedImage.save(Name)
def GetImageSize(needleImage):
needleFileObj = open(needleImage, 'rb')
needleImage = Image.open(needleFileObj)
needleImage = ImageOps.grayscale(needleImage)
needleWidth, needleHeight = needleImage.size
return needleWidth, needleHeight
def IsFocused():
return int(win32gui.GetForegroundWindow())