有没有一种方法可以检测某个像素的 RGB 值并在 if 条件下使用它?

Is there a way where one can detect RGB value of a certain pixel and use it in an if condition?

我正在尝试编写一个脚本来自动化工作中的无聊工作。为此,我使用 "pyautogui" 并且大部分脚本已完成。 OS 是 windows 顺便说一句。

我的问题是,我目前正在使用 time.sleep() 命令让脚本在流程的每个步骤之间工作,但最好检测某个像素的 RGB 值。

我目前做的是这样的;

pyautogui.click(req_management_find[0],req_management_find[1])    
time.sleep(2)
pyautogui.click(req_management_print[0],req_management_print[1])
while True:
         time.sleep(0.5)
         pix=pyautogui.pixel(1348,131)
         if pix[1] == 27 and pix[2] == 161 and pix[3] == 226:
           break
         else:
                 time.sleep(0.5)

pyautogui.click(req_print_close[0],req_print_close[1])

这只是永远等待,而不是跳出 while 循环。我已经使用 pyautogui.displayMousePosition() 读取了像素的 RGB 值。它通常是 (255, 255, 255)。在不确定的时间后,我尝试编写脚本的程序会弹出一个窗口,将像素的 RGB 从 (255, 255, 255) 更改为 (27, 161, 226)。

为什么我的代码没有检测到这个变化?

索引从 0 开始,所以它是:

if pix[0] == 27 and pix[1] == 161 and pix[2] == 226:

或者您可以使用以下方式使其更明确:

if pix.red == 27 and pix.green == 161 and pix.blue == 226:

当然,正如@Aditya Santoso 所建议的那样:

if pix == (27, 161, 226):