0

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?

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by woooee
0
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
0

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))))

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.