简体   繁体   English

如何使用Python的PIL在特定区域的屏幕上寻找特定像素的颜色?

[英]How can I look for a certain pixel colour on my screen, in a specific area using Python's PIL?

How can I look for a certain pixel colour on my screen, in a specific area using Python's PIL? 如何使用Python的PIL在特定区域的屏幕上寻找特定像素的颜色?

I need a function that will initiate a command in Python 2.7, if a certain pixel colour appears in a pre-designated area. 我需要一个函数,如果某个像素颜色出现在预先指定的区域中,它将在Python 2.7中启动命令。 I have tried the following code, with no avail: 我尝试了以下代码,但无济于事:

def game_one():
box = (x_pad+1, y_pad+1, x_pad+1179, y_pad+474)
im = ImageGrab.grab(box)
return im
rgb_im = im.convert('RGB')
for x in range(1055, 1120, 1):
    for y in range(20, 464, 1):
        pix = rgb_im.getpixel((x, y))
        if pix != (255,0,255):
            startPlaying()
            time.sleep(.2)
            clickbuttonone()
            print "Pixel Found!"
        else:
            print "Waiting for pixel to appear"

(The x_pad and y_pad are predefined screen variables - do not relate to the problem, but help to capture the whole screen) (x_pad和y_pad是预定义的屏幕变量-与问题无关,但有助于捕获整个屏幕)

ImageOps and Image are already imported, as is win32api and win32con. ImageOps和Image以及win32api和win32con已经导入。

Many thanks! 非常感谢!

To search until the pixel is found, you can use a loop. 要搜索直到找到像素,可以使用循环。 However, by nature it will be an infinite loop, which is bad practice. 但是,从本质上讲,这将是一个无限循环,这是不好的做法。 I'm adding a timeout of 120 seconds (2 minutes) which you can adjust as is reasonable. 我要添加120秒(2分钟)的超时时间,您可以根据需要进行调整。

import time
pf = False   #check if pixel was found
start_time = time.time()

def game_one():
    box = (x_pad+1, y_pad+1, x_pad+1179, y_pad+474)
    im = ImageGrab.grab(box)
    # return im
    rgb_im = im.convert('RGB')
    while (pf == False) and ((time.time()-start_time) < 120):
        for x in range(1055, 1120, 1):
            if pf == True:
                break
            for y in range(20, 464, 1):
                pix = rgb_im.getpixel((x, y))
                if pix != (255,0,255):
                    startPlaying()
                    time.sleep(.2)
                    clickbuttonone()
                    print "Pixel Found!"
                    pf = True
                    break
                else:
                    print "Waiting for pixel to appear"

Also, make sure you check your indentation--it's probably just for posting on this site but the function definition should not be on the same line as the rest of the function. 另外,请确保检查缩进-可能只是为了在此站点上发布,但功能定义不应与其余功能相同。

EDIT: Okay I think I found the major issue. 编辑:好的,我想我发现了主要问题。 Do you see where I commented out return im ? 看到我在哪里评论了return im吗? A return statement ends your function and nothing after it will be processed. return语句会终止您的函数 ,之后将不执行任何处理。 Your code wasn't even getting to your for loops. 您的代码甚至都没有进入for循环。 Try this. 尝试这个。 That's why it was printing that weird type thing, because that's what you were returning! 这就是为什么要打印奇怪的东西,因为那是您要返回的东西!

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

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