49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""
|
|
Screen vision — captures screenshots and identifies active/open windows.
|
|
Uses mss for fast capture and win32gui for window enumeration.
|
|
"""
|
|
import mss
|
|
import base64
|
|
import io
|
|
from PIL import Image
|
|
|
|
try:
|
|
import win32gui
|
|
WIN32_AVAILABLE = True
|
|
except ImportError:
|
|
WIN32_AVAILABLE = False
|
|
print("[JARVIS] pywin32 not available — window detection disabled")
|
|
|
|
def get_active_window_title() -> str:
|
|
if WIN32_AVAILABLE:
|
|
return win32gui.GetWindowText(win32gui.GetForegroundWindow())
|
|
return "Unknown"
|
|
|
|
def list_open_windows() -> list[str]:
|
|
if not WIN32_AVAILABLE:
|
|
return []
|
|
windows = []
|
|
def _enum(hwnd, _):
|
|
if win32gui.IsWindowVisible(hwnd):
|
|
title = win32gui.GetWindowText(hwnd)
|
|
if title:
|
|
windows.append(title)
|
|
win32gui.EnumWindows(_enum, None)
|
|
return windows
|
|
|
|
def capture_screen() -> str:
|
|
"""Returns base64-encoded PNG of the primary monitor."""
|
|
with mss.mss() as sct:
|
|
monitor = sct.monitors[1]
|
|
img = sct.grab(monitor)
|
|
pil_img = Image.frombytes("RGB", img.size, img.bgra, "raw", "BGRX")
|
|
buffer = io.BytesIO()
|
|
pil_img.save(buffer, format="PNG")
|
|
return base64.b64encode(buffer.getvalue()).decode()
|
|
|
|
def capture_screen_file(path: str = "screenshot.png") -> str:
|
|
"""Saves screenshot to file and returns the path."""
|
|
with mss.mss() as sct:
|
|
sct.shot(mon=1, output=path)
|
|
return path
|