-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscreenshot.py
149 lines (114 loc) · 4.64 KB
/
screenshot.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
import ctypes
from ctypes import windll,wintypes,create_string_buffer
from datetime import datetime
from PIL import Image
from logging import getLogger
from os.path import exists,basename
import numpy as np
logger_child_name = 'screenshot'
logger = getLogger().getChild(logger_child_name)
logger.debug('loaded screenshot.py')
from define import define
from resources import load_resource_serialized
SRCCOPY = 0x00CC0020
DIB_RGB_COLORS = 0
PW_CLIENTONLY = 1
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [
('biSize', wintypes.DWORD),
('biWidth', wintypes.LONG),
('biHeight', wintypes.LONG),
('biPlanes', wintypes.WORD),
('biBitCount', wintypes.WORD),
('biCompression', wintypes.DWORD),
('biSizeImage', wintypes.DWORD),
('biXPelsPerMeter', wintypes.LONG),
('biYPelsPerMeter', wintypes.LONG),
('biClrUsed', wintypes.DWORD),
('biClrImportant', wintypes.DWORD),
]
class RGBQUAD(ctypes.Structure):
_fields_ = [
('rgbRed', ctypes.c_byte),
('rgbGreen', ctypes.c_byte),
('rgbBlue', ctypes.c_byte),
('rgbReserved', ctypes.c_byte),
]
class BITMAPINFO(ctypes.Structure):
_fields_ = [
('bmiHeader', BITMAPINFOHEADER),
('bmiColors', ctypes.POINTER(RGBQUAD))
]
class Screen:
def __init__(self, np_value, filename):
self.np_value = np_value
self.original = Image.fromarray(np_value)
self.filename = filename
class Capture:
def __init__(self, width, height):
self.width = width
self.height = height
self.bmi = BITMAPINFO()
self.bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
self.bmi.bmiHeader.biWidth = self.width
self.bmi.bmiHeader.biHeight = self.height
self.bmi.bmiHeader.biPlanes = 1
self.bmi.bmiHeader.biBitCount = 24
self.bmi.bmiHeader.biCompression = 0
self.bmi.bmiHeader.biSizeImage = 0
self.screen = windll.gdi32.CreateDCW("DISPLAY", None, None, None)
self.screen_copy = windll.gdi32.CreateCompatibleDC(self.screen)
self.bitmap = windll.gdi32.CreateCompatibleBitmap(self.screen, self.width, self.height)
windll.gdi32.SelectObject(self.screen_copy, self.bitmap)
self.buffer = create_string_buffer(self.height * self.width * 3)
def shot(self, left, top):
windll.gdi32.BitBlt(self.screen_copy, 0, 0, self.width, self.height, self.screen, left, top, SRCCOPY)
windll.gdi32.GetDIBits(self.screen_copy, self.bitmap, 0, self.height, ctypes.pointer(self.buffer), ctypes.pointer(self.bmi), DIB_RGB_COLORS)
return np.array(bytearray(self.buffer)).reshape(self.height, self.width, 3)
def __del__(self):
windll.gdi32.DeleteObject(self.bitmap)
windll.gdi32.DeleteDC(self.screen_copy)
windll.gdi32.DeleteDC(self.screen)
logger.debug('Called Screenshot destuctor.')
class Screenshot:
xy = None
screentable = load_resource_serialized('get_screen')
np_value = None
def __init__(self):
self.checkscreens = [(screen, (areas['left'], areas['top']), Capture(areas['width'], areas['height']), self.screentable[screen]) for screen, areas in define.screens.items()]
self.capture = Capture(define.width, define.height)
def __del__(self):
for screen, pos, capture, value in self.checkscreens:
del capture
del self.capture
def get_screen(self):
if self.xy is None:
return None
results = []
for screen, pos, capture, value in self.checkscreens:
x = self.xy[0] + pos[0]
y = self.xy[1] + pos[1]
if np.sum(capture.shot(x, y)) == value:
results.append(screen)
if len(results) != 1:
return None
return results[0]
def shot(self):
if self.xy is None:
return False
self.np_value = self.capture.shot(self.xy[0], self.xy[1])[::-1, :, ::-1]
return True
def get_image(self):
if self.np_value is None:
return None
return Image.fromarray(self.np_value)
def get_resultscreen(self):
now = datetime.now()
filename = f"{now.strftime('%Y%m%d-%H%M%S-%f')}.png"
return Screen(self.np_value, filename)
def open_screenimage(filepath):
if not exists(filepath):
return None
image = Image.open(filepath).convert('RGB')
filename = basename(filepath)
return Screen(np.array(image), filename)