fix: screenshot thread safety - use fresh mss context per capture
- Remove persistent mss.mss() instance from AutoScreenshot - Use context manager 'with mss.mss() as sct:' for each capture - Fixes '_thread._local' object has no attribute 'srcdc' error - Allows screenshots to work from hotkey callbacks in background threads
This commit is contained in:
parent
b06b98e6cc
commit
53f7896dfa
|
|
@ -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}")
|
||||
|
|
|
|||
Loading…
Reference in New Issue