Need help returning a list to a function in python

advertisements

i need to do this Create 3 lists, one for red, one for green and one for blue, each containing 256 elements all initialized to 0 For every pixel in the image: Get the amount, r, of red from the pixel Increment position r in the red list Get the amount, g, of green from the pixel Increment position g in the green list Get the amount, b, of blue from the pixel Increment position b in the blue list Return the red, green and blue lists i wrote the code

def colour(): red=[0]*256 for x in range(0, getWidth(img)): for y in range (0, getHeight(img)): r= getPixel(img,getWidth(img) ,getHeight(img)) red.append(r) return (red) but for some reason i am unable to see the list


You appear to be getting the same value from the image each time. You should probably be using x and y instead of getWidth(img) in the getPixel function inside your loop.

Also, since you are appending in the loop (and not writing to a certain position in the list red) you should just initialize an empty loop and not a bunch of zeroes. I'd try:

def colour():
    red = []
    for x in range(getWidth(img)):
        for y in range (getHeight(img)):
            r= getPixel(img,x,y)
            red.append(r)
    return red