diff --git a/modules/auto_screenshot.py b/modules/auto_screenshot.py index 9fa9731..5a30c96 100644 --- a/modules/auto_screenshot.py +++ b/modules/auto_screenshot.py @@ -24,7 +24,6 @@ class AutoScreenshot: self.screenshot_dir = screenshot_dir or Path.home() / ".lemontropia" / "screenshots" self.screenshot_dir.mkdir(parents=True, exist_ok=True) - self.capture = mss.mss() self.session_screenshots: List[Path] = [] # Settings @@ -41,11 +40,13 @@ class AutoScreenshot: filepath = self.screenshot_dir / filename try: - monitor = self.capture.monitors[1] # Primary monitor - screenshot = self.capture.grab(monitor) - img = np.array(screenshot) - img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) - cv2.imwrite(str(filepath), img) + # Create fresh mss context for thread safety + with mss.mss() as sct: + monitor = sct.monitors[1] # Primary monitor + screenshot = sct.grab(monitor) + img = np.array(screenshot) + img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) + cv2.imwrite(str(filepath), img) self.session_screenshots.append(filepath) logger.info(f"Screenshot saved: {filepath}") @@ -63,11 +64,13 @@ class AutoScreenshot: filepath = self.screenshot_dir / filename try: - monitor = {"left": x, "top": y, "width": w, "height": h} - screenshot = self.capture.grab(monitor) - img = np.array(screenshot) - img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) - cv2.imwrite(str(filepath), img) + # Create fresh mss context for thread safety + with mss.mss() as sct: + monitor = {"left": x, "top": y, "width": w, "height": h} + screenshot = sct.grab(monitor) + img = np.array(screenshot) + img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) + cv2.imwrite(str(filepath), img) self.session_screenshots.append(filepath) logger.info(f"Region screenshot saved: {filepath}")