-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetWindow.py
More file actions
120 lines (96 loc) · 3.57 KB
/
Copy pathgetWindow.py
File metadata and controls
120 lines (96 loc) · 3.57 KB
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
import psutil
import win32gui
import win32ui
import win32process
import ctypes
import numpy as np
import cv2
import time
def enum_window_callback(hwnd, pid_and_windows):
pid, windows = pid_and_windows
tid, current_pid = win32process.GetWindowThreadProcessId(hwnd)
if pid == current_pid and win32gui.IsWindowVisible(hwnd):
windows.append(hwnd)
def get_window(w_name):
try:
instansess = [item for item in psutil.process_iter() if item.name() == w_name]
_PIDs = [item.pid for item in instansess]
if instansess:
window_HWNDs = []
for pid in _PIDs:
win32gui.EnumWindows(enum_window_callback, [pid, window_HWNDs])
return (window_HWNDs, _PIDs)
else:
print("Process ", w_name, " was not found.")
return ([], [])
except Exception as e:
print(e)
return ([], [])
def resize(im, coef): # resizes the image by coef
MAX_SIZE = 2000
MIN_SIZE = 10
width = int(im.shape[1] * coef)
height = int(im.shape[0] * coef)
dsize = (width, height)
if width > MIN_SIZE or height > MIN_SIZE or height < MAX_SIZE or width < MAX_SIZE:
return cv2.resize(im, dsize)
else:
print("Resized image is to small or to big. No resizing was done.")
return im
def process_image(im): # processes the image the way you want
image2 = resize(im, 0.9)
hsv = cv2.cvtColor(image2, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, np.array([86, 165, 254]), np.array([93, 174, 256])) # masks specific color range
nonzero = cv2.findNonZero(mask)
x, y, w, h = cv2.boundingRect(nonzero)
temp = mask[y:y+h, x:x+w]
res = cv2.copyMakeBorder(temp.copy(), 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=(0,0,0))
res = cv2.bitwise_not(res)
# blank_image = image2.copy()
# blank_image[:] = (0, 0, 255)
# res = cv2.bitwise_and(blank_image, blank_image, mask=mask) # applies mask to the image and shows result on black bg
return res
def win_create_bitmap(window, w, h, left, top):
# hwndDC = win32gui.GetDC(window)
hwndDC = win32gui.GetWindowDC(window)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
result = ctypes.windll.user32.PrintWindow(window, saveDC.GetSafeHdc(),
1) # BOOL PrintWindow(HWND hwnd,HDC hdcBlt,UINT nFlags;
if result == 1:
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
img = np.fromstring(bmpstr, dtype='uint8')
img.shape = (h, w, 4)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(window, hwndDC)
if result:
return img, result
else:
return None, result
def get_image_(window):
# print("|DEBUG| Current window HNDL: ", window)
bbox = win32gui.GetWindowRect(window)
left, top, right, bot = bbox
w = right - left
h = bot - top
if (w > 0 and h > 0):
try:
win_img, bool_img = win_create_bitmap(window, w, h, left, top)
if bool_img:
cv_img = cv2.cvtColor(win_img, cv2.COLOR_RGBA2RGB)
result = process_image(cv_img)
# cv2.imshow("screen",result)
# cv2.waitKey()
return result
except Exception as e:
print(e)
return []
else:
print(" ... Ops!Window to small. \n")
return []