28 lines
801 B
Python
28 lines
801 B
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import cv2
|
|
import mss
|
|
import numpy as np
|
|
|
|
from botty_next.capture.window import WindowRegion
|
|
|
|
|
|
class MssCaptureBackend:
|
|
def grab(self, region: WindowRegion | None = None) -> np.ndarray:
|
|
with mss.mss() as screen_capture:
|
|
monitor = region.as_mss_monitor() if region else screen_capture.monitors[1]
|
|
shot = screen_capture.grab(monitor)
|
|
|
|
bgra = np.asarray(shot)
|
|
return cv2.cvtColor(bgra, cv2.COLOR_BGRA2BGR)
|
|
|
|
|
|
def save_frame(frame: np.ndarray, output_path: str | Path) -> Path:
|
|
output = Path(output_path)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
if not cv2.imwrite(str(output), frame):
|
|
raise RuntimeError(f"failed to write screenshot: {output}")
|
|
return output
|