-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenCapture.py
101 lines (84 loc) · 3.41 KB
/
ScreenCapture.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
import numpy as np
import threading
from mss import mss
from PIL import Image
import time
class ScreenCapture:
''' def __init__(self, x, y, grabzone):
self.x, self.y, self.grabzone = x, y, grabzone
self.screen = np.zeros((grabzone, grabzone, 3), np.uint8)
self.pillow = None
self.lock = threading.Lock()
self.frame_count = 0
self.start_time = time.time()
self.start()
def start(self):
thread = threading.Thread(target=self.update, daemon=True)
thread.start()
def update(self):
while True:
with mss() as sct, self.lock:
monitor = {"top": self.y, "left": self.x, "width": self.grabzone, "height": self.grabzone}
self.pillow = sct.grab(monitor)
#img = self.pillow
#mode = img.mode
#print("Color Mode: RGB")
self.screen = np.array(self.pillow)
#print(self.screen.shape)
self.frame_count += 1
elapsed_time = time.time() - self.start_time
if elapsed_time >= 1:
fps = self.frame_count / elapsed_time
print(f" FPS: {fps:.2f}", end="\r")
self.frame_count = 0
self.start_time = time.time()'''
def __init__(self):
self.screen = None
self.pillow = None
self.lock = threading.Lock()
self.frame_count = 0
self.start_time = time.time()
self.start()
def start(self):
thread = threading.Thread(target=self.update, daemon=True)
thread.start()
def update(self):
while True:
with mss() as sct, self.lock:
monitor = sct.monitors[0]
monitor_width = monitor["width"]
monitor_height = monitor["height"]
# full monitor
monitor = {"top": 0, "left": 0, "width": monitor_width, "height": monitor_height}
self.pillow = sct.grab(monitor)
self.screen = np.array(self.pillow)
self.frame_count += 1
elapsed_time = time.time() - self.start_time
if elapsed_time >= 1:
fps = self.frame_count / elapsed_time
print(f" FPS: {fps:.2f}", end="\r")
self.frame_count = 0
self.start_time = time.time()
# return np.array
def get_screen(self):
with self.lock:
return self.screen
# return pillow opject
def get_pillow(self):
with self.lock:
if self.pillow is not None:
return self.pillow
else:
# pillow empty
return Image.new("RGB", (self.grabzone, self.grabzone))
def get_actual_screen_dimensions(self):
with mss() as sct:
screen_info = sct.monitors[0]
screen_width = screen_info["width"]
screen_height = screen_info["height"]
return screen_width, screen_height
'''def get_pillow(self):
with self.lock:
#return self.pillow
#return Image.frombytes("RGB", self.pillow.size, self.pillow.bgra, "raw", "BGRX") self.x
return Image.frombytes("RGB", self.pillow.size, self.pillow.bgra, "raw", "BGRX") '''