0

我想编写一个函数来打印出图片的颜色值 RGB。图片要么全部着色为红色绿色黄色或白色。

我所拥有的是以下内容:

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 值的正确方法吗?如果图片包含不同的颜色,我相信第二个代码不准确。

4

2 回答 2

1

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

def findColor():
    pic=takePicture()
    for pix in getPixels(pic):
        r = getRed(pix)
        g = getGreen(pix)
        b = getBlue(pix)
        print(r,g,b) 
于 2014-10-24T19:15:53.993 回答
0

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

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

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

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

于 2018-02-08T07:31:13.037 回答