-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_screen.py
More file actions
41 lines (33 loc) · 1.43 KB
/
record_screen.py
File metadata and controls
41 lines (33 loc) · 1.43 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
import time
from pathlib import Path
import mss
class ScreenCapture:
def __init__(self, output_dir='captures'):
self.output_dir = Path(output_dir)
self.output_dir.mkdir(exist_ok=True)
def capture_monitor(self, monitor_index=1, filename='screenshot.png'):
"""Capture a specific monitor."""
with mss.mss() as sct:
if monitor_index >= len(sct.monitors):
raise ValueError(f"Monitor {monitor_index} not found")
screenshot = sct.grab(sct.monitors[monitor_index])
output_path = self.output_dir / filename
mss.tools.to_png(screenshot.rgb, screenshot.size, output=str(output_path))
return output_path
def capture_region(self, left, top, width, height, filename='region.png'):
"""Capture a specific region."""
region = {'left': left, 'top': top, 'width': width, 'height': height}
with mss.mss() as sct:
screenshot = sct.grab(region)
output_path = self.output_dir / filename
mss.tools.to_png(screenshot.rgb, screenshot.size, output=str(output_path))
return output_path
def list_monitors(self):
"""List available monitors."""
with mss.mss() as sct:
return sct.monitors
# Usage
capture = ScreenCapture()
print("Monitors:", capture.list_monitors())
capture.capture_monitor(1, 'monitor1.png')
capture.capture_region(0, 0, 500, 500, 'region.png')