在图像中搜索颜色

在图像中搜索颜色

本文介绍了在图像中搜索颜色.返回X,Y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找寻找图像中特定颜色(屏幕截图)并返回颜色位置(x,y)的方法.我做了一些尝试,但是没有设法进行正确的搜索.结果应该是找到该颜色的第一个像素.

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.

我相信PIL可能会有所帮助.所以我尝试了类似的方法,现在的问题是它返回用该颜色找到的每个位置:

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:

已修复:

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.

推荐答案

返回第一个匹配项的结果,而不是继续循环.

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

这篇关于在图像中搜索颜色.返回X,Y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:04