Search image for color. Return X, Y

帅比萌擦擦* 提交于 2021-02-07 10:01:59

问题


I've been looking all over for a way to find a specific color in a image (screen capture), and return the the position of of the color (x,y). I've had some tries, but not managed to do a proper search. The result should be the first pixel found with that color.

I tought maybe PIL will be to any help. So I tried something like this, problem now is that it returns EVERY position, found with that color:

Fixed:

def FindColor(r,g,b):
    image = ImageGrab.grab()
    for x in range(1, 400):
        for y in range(1,400):
            px = image.getpixel((x, y))
            if px[0] == r and px[1] == g and px[2] == b:
                return(x,y)

And, I need to replace the loops range, with the pictures width/height.


回答1:


return the result at the first match, instead of continuing the loop.



来源:https://stackoverflow.com/questions/8862387/search-image-for-color-return-x-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!