[python]检测屏幕像素点颜色

import pyautogui
from PIL import ImageGrab
import keyboard

def get_mouse_pixel_color():
    # 获取鼠标当前位置
    x, y = pyautogui.position()
    
    # 获取屏幕图像
    screen = ImageGrab.grab()
    
    # 获取鼠标位置像素的颜色
    color = screen.getpixel((x, y))
    
    return x, y, color

if __name__ == "__main__":
    print("按 'g' 键结束检测")
    try:
        while True:
            if keyboard.is_pressed('g'):
                print("检测结束")
                break

            x, y, color = get_mouse_pixel_color()
            print(f"鼠标位置: ({x}, {y}), 像素颜色: {color}")
    except KeyboardInterrupt:
        print("检测中断")