Search code examples
pythonmyro

Finding RGB values


I want to write a function that prints out the color values, RGB, of a picture. The pictures are either colored all red, green, yellow or white.

What I have is the following:

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)

Or a code that gives me similar values as above:

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

Are these codes a correct way of getting the RGB values? I believe the second code isn't accurate if the picture contained different colors.


Solution

  • If you just want to print the rgb value for each individual pixel, your second code block will work if you fix the indentation.

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