import sys
import json
import base64
from io import BytesIO

def capture_screen():
    try:
        import pyautogui
        screenshot = pyautogui.screenshot()
        buffered = BytesIO()
        screenshot.save(buffered, format="PNG")
        img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
        width, height = screenshot.size
        print(json.dumps({
            "success": True,
            "width": width,
            "height": height,
            "base64": img_str
        }))
    except Exception as e:
        print(json.dumps({"success": False, "error": str(e)}))

if __name__ == "__main__":
    capture_screen()
