Hi, Im currently taking an introductory course in python and i need help with some stuff.

Basically for an assignment the first function we have is supposed to get an an average red value of all the pixels in the image. red being the R in (RGB). So basically the red pigment value. So basically i need to find the R value for all the pixels in the image and then add them up and divide by the total number of pixels. Now we are using the pygraphics module for this so this is what i have so far.

import media

def red_average():
    pic = media.load_picture(media.choose_file())
    for p in media.get_pixels(pic):
        x = (sum(range(media.get_red(p))))
        print x

Now instead of giving me 1 big number it gives me a list of numbers instead when i call the function. Basically does that mean its adding up all the red values from each row of pixels and displaying them?

import media

def red_average():
    pic = media.load_picture(media.choose_file())
    total = 0
    for p in media.get_pixels(pic):
        x = (sum(range(media.get_red(p))))
        total +=x
    print total

To debug code like this, you have to break it down into individual steps to know where the error lies.

for p in media.get_pixels(pic):
        x = (sum(range(media.get_red(p))))
        print x
##
##   with minimum/terse print statements, it becomes
    ctr = 0
    for p in media.get_pixels(pic):
        if ctr < 5:     ## don't print the entire picture
            ctr += 1
            y = media.get_red(p)
            print type(y), y
            z = range(y)
            print z
            x = sum(z)
#        x = (sum(range(media.get_red(p))))
            print type(x), x

Edit: I just noticed the extra set of parens, which creates a tuple.

here                         here
x = (sum(range(media.get_red(p))))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.