【问题标题】:Finding RGB values查找 RGB 值
【发布时间】:2018-02-08 16:19:09
【问题描述】:

我想编写一个函数来打印图片的颜色值 RGB。图片全部为redgreenyellow 或白色。

我有以下内容:

def findColor():
    pic=takePicture()
    red = 0   
    green = 0
    blue = 0 
    size = getWidth(pic)*getHeight(pic)
    for pix in getPixels(pic):
        r = getRed(pix)
        g = getGreen(pix)
        b = getBlue(pix)
        red = red + r
        green = green + g
        blue = blue + b 
    print(red//size,green//size,blue//size)

或者给我与上面类似的值的代码:

def findColor():
    pic=takePicture()
    for pix in getPixels(pic):
        r = getRed(pix)
        g = getGreen(pix)
        b = getBlue(pix)
    print(r,g,b)   

这些代码是获取 RGB 值的正确方法吗?如果图片包含不同的颜色,我认为第二个代码不准确。

【问题讨论】:

  • "这些代码是获取 RGB 值的正确方法吗?"让我用另一个问题来回答这个问题。当你运行它们时,你得到你想要的输出了吗?
  • @Kevin 是的,如果我像链接的图片一样设置限制。不,如果没有限制,例如图片是红色、黑色和紫色的混合体。我想知道第一个代码是否正确。
  • 第一个打印图像中所有像素的平均值。第二个打印图像右下角像素的颜色。如果您的图像只包含一种颜色,那么它们将打印相同的内容。
  • @Kevin Hm,那么找到 RGB 值的正确方法将是我的第一个代码?
  • 我不清楚“找到 RGB 值”是什么意思,所以我无法真正回答。

标签: python myro


【解决方案1】:

如果您只想为每个单独的像素打印 rgb 值,如果您修复缩进,您的第二个代码块将起作用。

def findColor():
    pic=takePicture()
    for pix in getPixels(pic):
        r = getRed(pix)
        g = getGreen(pix)
        b = getBlue(pix)
        print(r,g,b) 

【讨论】:

    【解决方案2】:

    许多人迟到了原来的帖子。

    如果图片是形状类似于(R,C,3) R-> Rows/Height 、C -> Columns/Width 和 3 个通道 (RGB) 的 ndarray

    这篇文章Python numpy array with multiple conditions to iterate over image 是您可能正在搜索的one line solution

    Red,Green,Blue = img[...,0],img[...,1],img[...,2]

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 1970-01-01
      • 2015-05-05
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2017-10-26
      • 1970-01-01
      相关资源
      最近更新 更多