【问题标题】:How to check if a PNG has a certain RGB color using Python?如何使用 Python 检查 PNG 是否具有某种 RGB 颜色?
【发布时间】:2020-07-12 16:23:46
【问题描述】:

我有一个.png 文件,我想扫描图像以检查其中是否有某个 RGB 值。例如,假设我有一张图像,并且想要检查 RGB 值 (255, 0, 0) 是否在图像中的某个位置。我将如何在 Python 中执行此操作?谢谢!

【问题讨论】:

    标签: python python-3.x image colors png


    【解决方案1】:

    我建议你使用PIL-GetpixelPIL-Getdata

    from PIL import Image
    
    im = Image.open('whatever.png').convert("RGB")
    
    # get pixels
    pixels = [im.getpixel((i, j)) for j in range(im.height) for i in range(im.width)]
    
    # or
    pixels = [i for i in im.getdata()]
    
    #check if tuple of pixel value exists in array-pixel
    
    print((255, 0, 0) in pixels) #True if exists, False if it doesn't
    

    【讨论】:

    • 嗯...这似乎是一个不错的方法,但是对于更大的文件需要多长时间?
    • 在我这样做之前最好在 png 上使用缩略图方法,以提高运行时间。
    • 这是 PIL 库的文档。要回答你的问题,这取决于你有多少内存,你的 png 文件有多大。我不知道你在用图像做什么,但如果可能的话,你可以一点一点地阅读它,并使用 python 生成器,一次看到你的图像一个像素值。
    • 我在一张完全是红色的 (255, 0, 0) 图像上进行了测试,但它仍然返回 false...您在代码中输入错误了吗?
    • 这样,您的答案将被标记为最佳 :D 谢谢!
    【解决方案2】:

    这应该可行..

    import cv2
    import numpy as np
    
    img = cv2.imread(r'circle.png')
    ind = np.where((img[:, :, 0]==255) & (img[:, :, 1]==0) & (img[:, :, 2]==0))
    answer = list(zip(ind[0], ind[1]))
    print(answer) # Prints row and column indices in tuples
    

    【讨论】:

    • 非常感谢!你能解释一下每一行的作用吗?
    • 如何更改我要查找的颜色?
    • 为什么要导入torch?此外,考虑到语法,answer = list(zip(*ind)) 是首选。
    • 前两行用于导入。 cv2 用于 OpenCV 包。参考:docs.opencv.org/master/d0/de3/tutorial_py_intro.html
    • 第 4 行读取图像。参考:docs.opencv.org/2.4/modules/highgui/doc/…。图像被读取为一个 numpy nd 数组,形状为 [height, width, channels]。通道维度表示图像中使用的原色。第五行挑选出在第 0、第 1 和第 2 位置具有所需值的 numpy 数组的索引。相应地更改值以找到相关像素。第六行将索引收集到(行,列)元组中,因此它是具有所需像素的元组列表。然后打印出来。
    【解决方案3】:

    您可以使用cv2 包加载图像,并使用numpy 将其作为数组进行搜索:

    import cv2
    import numpy as np
    
    img = cv2.imread('one.png')
    
    pixel = img[801,600]
    
    print (pixel) # pixel value i am searching for
    
    def search_array():
        pixel_tile = np.tile(pixel, (*img.shape[:2], 1))
        diff = np.sum(np.abs(img - pixel_tile), axis=2)
        print("\n".join([f"SUCCESS - {idx}" for idx in np.argwhere(diff == 0)]))
    
    if __name__ == "__main__":
        search_array()
    

    摘自我的回答here

    【讨论】:

    • 非常感谢!您能否解释一下这些线条的作用,以便我更好地理解发生了什么,这样我就可以使它们适应以后的问题:D
    • @ThatComputerGuy 是的,它会从您想要查找的像素中减去图像中的所有像素。然后,它报告它在绝对差中找到的所有零的位置。 np.tile 将像素扩展到所有值,np.sum 将 RGB 值折叠到一个通道(绝对差意味着零只是零的总和),np.argwhere 在值为零的情况下给出位置.
    • 如何更改我正在寻找的颜色?
    【解决方案4】:

    我有 numpy 的替代解决方案。

    import cv2
    import numpy as np
    
    im = cv2.imread('your_image.png')
    
    print('Your color is in the image', (im == (255, 0, 0)).all(axis=-1).max())
    

    test == (255, 0, 0) 检查是否有任何颜色通道等于元组(255, 0, 0) 的相应条目。如果所有这 3 个条目评估为真,则相应位设置为 True。如果结果数组test.shape[0:2] 中的任何位为真,那么这就是结果。

    【讨论】:

      猜你喜欢
      • 2016-06-18
      • 2010-10-15
      • 1970-01-01
      • 2013-02-28
      • 2018-02-10
      • 2018-12-01
      • 2011-07-25
      • 1970-01-01
      • 2015-03-15
      相关资源
      最近更新 更多