首页 > 解决方案 > 屏幕评估问题和简单的 if 语句

问题描述

我是stackoverflow的新手,但有一个愚蠢的问题:

如果我打开命令提示符,我想让我的电脑做某事。我有其他计划,但目前它只是打印出来是否打开。为什么这段代码不起作用???计算机甚至打印出触发值(12、12、12)。我的 if 语句有问题吗?

提前致谢

输出:

12.12.12
command prompt not opened
12.12.12
command prompt not opened
12.12.12
command prompt not opened
12.12.12
command prompt not opened
12.12.12
command prompt not opened
12.12.12
command prompt not opened
12.12.12
command prompt not opened
12.12.12
command prompt not opened
from PIL import ImageGrab, Image
import time

pixel = (1713, 978) #pixel for evaluation

while True:
    img = ImageGrab.grab() #grabs image (like a screenshot)

    color = img.getpixel(pixel) #reads colors of this pixel

    red = str(color[0]) #saves colors in three variables to make evaluation easier
    green = str(color[1])
    blue = str(color[2])

    print(red + "." + green + "." + blue) #prints color in rgb

    if red == 12 and green == 12 and blue == 12: #if all three colors are 12 -> print that cmd is opened
        print("command promt opened")
        time.sleep(5)
    else: #else print that cmd isn't opened
        print("command prompt not opened")```

标签: pythonautomationpython-imaging-library

解决方案


这是解决方案:

if int(red) == 12 and int(green) == 12 and int(blue) == 12:

推荐阅读