-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_capture.py
169 lines (139 loc) · 5.64 KB
/
window_capture.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
import numpy as np
from time import sleep
from threading import Thread, Lock
# Screenshot
import win32gui, win32ui, win32con
# TODO: USE MORE GENERIC VALUES
# Minimap constants
MINIMAP_TOP_LEFT_POSITION_X = 1700
MINIMAP_TOP_LEFT_POSITION_Y = 700
MINIMAP_WIDTH = 300
MINIMAP_HEIGHT = 300
# Fullscreen constants
FULLWINDOWS_TOP_LEFT_POSITION_X = 0
FULLWINDOWS_TOP_LEFT_POSITION_Y = 0
FULLWINDOWS_WIDTH = 1920
FULLWINDOWS_HEIGHT = 1080
class WindowCapture:
# Thread attribut
stopped = True
lock = None
screenshot = None
# Monitor width and height
w = 0
h = 0
hwnd = None
cropped_x = 0
cropped_y = 0
offset_x = 0
offset_y = 0
def __init__(self, window_name=None):
self.lock = Lock()
if window_name is None:
self.hwnd = win32gui.GetDesktopWindow()
else:
self.hwnd = win32gui.FindWindow(None, window_name)
if not self.hwnd:
raise Exception("Window not found {}".format(window_name))
# Get the window size
'''
window_rect = win32gui.GetWindowRect(self.hwnd)
w = window_rect[2] - window_rect[0]
h = window_rect[3] - window_rect[1]
'''
w = MINIMAP_WIDTH
h = MINIMAP_HEIGHT
# Crop the window border
top_border_pixel_size = 18 # title bar
bottom_border_pixel_size = 20 # tasks bar
right_border_pixel_size = 0
left_border_pixel_size = 0
self.w = w - right_border_pixel_size - left_border_pixel_size
self.h = h - top_border_pixel_size - bottom_border_pixel_size
'''
self.cropped_x = window_rect[0] + left_border_pixel_size
self.cropped_y = window_rect[1] + top_border_pixel_size
'''
self.cropped_x = MINIMAP_TOP_LEFT_POSITION_X + left_border_pixel_size
self.cropped_y = MINIMAP_TOP_LEFT_POSITION_Y + top_border_pixel_size
# Set the cropped coordinates offset
# To translate screenshot images into actual screen positions
'''
self.offset_x = window_rect[0] + self.cropped_x
self.offset_y = window_rect[1] + self.cropped_y
'''
self.offset_x = 0 + self.cropped_x
self.offset_y = 0 + self.cropped_y
def get_screenshot(self):
# Get the window image data
# https://stackoverflow.com/questions/3586046/fastest-way-to-take-a-screenshot-with-python-on-windows/3586280#3586280
wDC = win32gui.GetWindowDC(self.hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (self.w, self.h) , dcObj, (self.cropped_x, self.cropped_y), win32con.SRCCOPY)
# Save the screenshot
# dataBitMap.SaveBitmapFile(cDC, "img/debug.png")
# https://stackoverflow.com/questions/41785831/how-to-optimize-conversion-from-pycbitmap-to-opencv-image
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = (self.h, self.w, 4)
# Free Resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(self.hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
# Drop the alpha channel
img = img[...,:3]
# Make image C_CONTIGIOUS to avoid errors like:
# File ... in draw_rectangles
# TypeError: an integer is required (got type tuple)
img = np.ascontiguousarray(img)
return img
# Translate a pixel position on a screenshot image to a pixel position on the screen
# @param : pos = (x, y)
# Warning : if you move the window being captured after execution was started
# this will return incorrect coordinates
# because the window position is only calculated in __init__ constructor
def get_screen_position(self, pos):
return (pos[0] + self.offset_x, pos[1] + self.offset_y)
# Returns (width, height) the resolution of the game screen
def get_game_resolution(self):
return (self.w, self.h)
# THREAD METHODS
def start(self):
self.stopped = False
t = Thread(target=self.run)
t.start()
def stop(self):
self.stopped = True
def run(self):
# TODO: while loop can be slowed down
while not self.stopped:
print("take a screenshot")
screenshot = self.get_screenshot()
# lock the thread while updating the result
self.lock.acquire()
self.screenshot = screenshot
self.lock.release()
sleep(2)
# STATIC METHODS
@staticmethod
def show_window_names():
# https://stackoverflow.com/questions/55547940/how-to-get-a-list-of-the-name-of-every-open-window
def winEnumHandler(hwnd, ctx):
if win32gui.IsWindowVisible(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
win32gui.EnumWindows(winEnumHandler, None)
@staticmethod
def get_window_names():
# https://stackoverflow.com/questions/55547940/how-to-get-a-list-of-the-name-of-every-open-window
windows = []
def winEnumHandler(hwnd, ctx):
if win32gui.IsWindowVisible(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
windows.append(win32gui.GetWindowText(hwnd))
win32gui.EnumWindows(winEnumHandler, None)
return windows