forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture.py
More file actions
30 lines (27 loc) · 883 Bytes
/
Copy pathcapture.py
File metadata and controls
30 lines (27 loc) · 883 Bytes
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
import mss
from PIL import Image
try:
from pyvirtualdisplay import Display
_has_virtual = True
except ImportError:
_has_virtual = False
def start_virtual_display(width=1280, height=720):
if _has_virtual:
display = Display(visible=0, size=(width, height))
display.start()
return display
return None
def capture_screen(region=None):
try:
with mss.mss() as sct:
if region is None:
if len(sct.monitors) < 2:
raise RuntimeError("No monitors available for capture")
monitor = sct.monitors[1]
else:
monitor = region
sct_img = sct.grab(monitor)
img = Image.frombytes('RGB', sct_img.size, sct_img.rgb)
return img
except Exception as e:
raise RuntimeError(f"Screen capture failed: {e}") from e