python - Count pixels of color in JES (Jython) -


i trying pull overall pixel count of r, g, b, black, & white values in premade picture. picture has 100 red, 100 green, 100 blue, 100 black, , 100 white.

i have started code, reason seems if code counting 1 pixel.. jython has predefined 16 colors, using red, blue, green datatypes.

here have 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 pixels...")   px in getpixels( pic ):    r = getred(px)    g = getgreen(px)    b = getblue(px)    if px (255,0,0): #error here      numr += 1      printnow("you have " + numr + " red pixels")  show(pic) 

unsure why not working..

you don't need colors separately. can go getcolor(px)-function.

furthermore there no function printnow(str) in python. long function not part of package use, need use print(str)

the function getcolor returns object color(255,0,0) compare this, can't compare against tuple want use distance function jes. therefore need make color object comparison red = makecolor(255,0,0) , compare against this. possible output distance function ranges 0 (exact same color) ~441.7 (black compared white).

so try this:

red = makecolor(255,0,0) green = makecolor(0,255,0) blue = makecolor(0,0,255)  px in getpixels( pic ):    color = getcolor(px)    if distance(color, red) == 0:        numr += 1    elif distance(color, green) == 0:        numg += 1    elif distance(color, blue) == 0:        numb += 1  print("you have " + numr + " red pixels") print("you have " + numg + " green pixels") print("you have " + numb + " blue pixels") 

i guessed need number in total after counting. if want output number while iterating, put print in loop.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -