0

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.

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Robben
  • 251
  • 3
  • 11
  • 2
    "Are these codes a correct way of getting the RGB values?" Let me answer that question with another question. When you run them, do you get the output that you want? – Kevin Oct 24 '14 at 19:14
  • @Kevin Yes, if I set restrictions like the pictures I linked. And no, if the restrictions aren't there, for example if the picture was a mixture of red, black and purple. I will like to know if the first code is the correct way. – Robben Oct 24 '14 at 19:18
  • The first one prints the average of all pixels in the image. The second one prints the color of the bottom-right pixel in the image. If your image contains exactly one color, then they'll both print the same thing. – Kevin Oct 24 '14 at 19:20
  • @Kevin Hm, so the correct way of finding the RGB values will then be my first code? – Robben Oct 24 '14 at 19:20
  • It's not clear to me what "finding the RGB values" means, so I can't really answer that. – Kevin Oct 24 '14 at 19:21
  • @Kevin I should rephrase then. Lets say the function determines the linked picture's color and prints out either red, green, yellow or white. Both of my codes do that, but which code will be the more accurate? I am sorry if it doesn't make any sense, I am still a newb to coding. – Robben Oct 24 '14 at 19:28
  • If your image contains exactly one color, then they're both 100% accurate. Neither one will be more accurate than the other. – Kevin Oct 24 '14 at 19:29
  • @Kevin But if the pictures didn't contain one color? – Robben Oct 24 '14 at 19:30
  • Then "find the color of an image with more than one color" is an under-defined problem, and there's not enough information to solve it. What is the color of an image of a red flower with a green stem on a yellow background? Whichever color appears most often? The average of the three colors? The color that appears in the center square when you divide the image using the [rule of thirds](http://en.wikipedia.org/wiki/Rule_of_thirds)? – Kevin Oct 24 '14 at 19:37
  • @Kevin Whoa! I never thought about that. This is more complicated than I thought. But you helped me clarify my issues. Thank you very much! – Robben Oct 24 '14 at 19:44

2 Answers2

1

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) 
Kevin
  • 74,910
  • 12
  • 133
  • 166
0

Many be late for the original post.

In case the pic is a ndarray with shape like (R,C,3) R-> Rows/Height , C -> Columns/Width and 3 channels (RGB)

This article Python numpy array with multiple conditions to iterate over image is the one line solution you might be searching for

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

Santhosh
  • 1,554
  • 1
  • 13
  • 19