-
Notifications
You must be signed in to change notification settings - Fork 65
/
window_capture.py
53 lines (41 loc) · 1.71 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
import win32api, win32con, win32gui, win32ui
import numpy as np
import cv2
class WindowCapture:
def __init__(self, name, width=1920, height=1080):
self.WINDOW_NAME = name
self.DEFAULT_MONITOR_WIDTH = width
self.DEFAULT_MONITOR_HEIGHT = height
self.hwnd = win32gui.FindWindow(None, self.WINDOW_NAME)
self.genshin_window_rect = win32gui.GetWindowRect(self.hwnd)
self.ofx=0
self.ofy=0
def set_offset(self, ofx, ofy):
self.ofx=ofx
self.ofy=ofy
def cap_box(self, box=None, resize=None):
return self.cap(area=[box[0], box[1], box[2]-box[0], box[3]-box[1]], resize=resize)
def cap(self, area=None, resize=None):
if area is None:
area=[0,0,self.DEFAULT_MONITOR_WIDTH, self.DEFAULT_MONITOR_HEIGHT]
w, h = area[2:]
wDC = win32gui.GetWindowDC(self.hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (w, h), dcObj, (area[0]+self.ofx, area[1]+self.ofy), win32con.SRCCOPY)
# dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.frombuffer(signedIntsArray, dtype="uint8")
img.shape = (h, w, 4)
img=cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
# Free Resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(self.hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
if resize is not None:
img=cv2.resize(img, (int(resize[0]), int(resize[1])))
return img