简体   繁体   English

如何在 Python 中检测屏幕像素的颜色

[英]How to detect a screen's pixel's color in Python

I want to know how we can detect a pixel's color using pyautogui doesnt work as it gives a error OSError: windll.user32.ReleaseDC failed: return 0 is there any alternatives to detecting a pixel's color我想知道我们如何使用 pyautogui 检测像素的颜色不起作用,因为它给出了错误OSError: windll.user32.ReleaseDC failed: return 0是否有其他方法可以检测像素的颜色

And my code is:我的代码是:

import pyautogui
from time import sleep as wait
import keyboard

while keyboard.is_pressed('q') == False:
    if pyautogui.pixel(565, 171)[0] == 0:
        pyautogui.click(565, 171)
        wait(0.01)
    if pyautogui.pixel(670, 171)[0] == 0:
        pyautogui.click(670, 171)
        wait(0.01)
    if pyautogui.pixel(769, 171)[0] == 0:
        pyautogui.click(769, 171)
        wait(0.01)
    if pyautogui.pixel(873, 171)[0] == 0:
        pyautogui.click(873, 171)
        wait(0.01)

I fixed it by handling the error我通过处理错误来修复它

import pyautogui
from time import sleep as wait
import keyboard

while keyboard.is_pressed('q') == False:
    try:
        if pyautogui.pixel(565, 171)[0] == 0:
            pyautogui.click(565, 171)
            wait(0.01)
        if pyautogui.pixel(670, 171)[0] == 0:
            pyautogui.click(670, 171)
            wait(0.01)
        if pyautogui.pixel(769, 171)[0] == 0:
            pyautogui.click(769, 171)
            wait(0.01)
        if pyautogui.pixel(873, 171)[0] == 0:
            pyautogui.click(873, 171)
            wait(0.01)
    except Exception as e:
        print(e)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM