简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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