I am trying to pull the overall pixel count of the R, G, B, Black, & White values in a premade picture. This picture has 100 Red, 100 Green, 100 Blue, 100 Black, and 100 white.
I have started with my code, but for some reason it seems as if my code is only counting 1 pixel.. Jython has predefined 16 colors, so I am using the red, blue, green datatypes.
Here is what I have so far:
def main():
file = pickAFile( )
pic = makePicture( file )
pxRed = 0
pxGreen = 0
pxBlue = 0
numR = 0
numG = 0
numB = 0
printNow("Now let's count some pixels...")
for px in getPixels( pic ):
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
If px is (255,0,0): #error here
numR += 1
printNow("You have " + numR + " red pixels")
show(pic)
unsure why this is not working..
You don't need to get the colors separately. You can go with the
getColor(px)
-function.Furthermore there is no function
printNow(str)
in python. So as long as this function is not part of any package you use, you need to useprint(str)
The function
getColor
returns an Object likeColor(255,0,0)
to compare this, you can't just compare against a Tuple but want to use thedistance
function from JES. Therefore you need to make aColor
object for comparison likered = makeColor(255,0,0)
and compare against this. The possible output from thedistance
function ranges from 0 (exact same color) to ~441.7 (black compared with white).So try it like this:
I guessed you need the number in total after counting. If you want to output the number while iterating, just put the print back in the loop.