The simple CS106AP "SimpleImage" code provides basic digital image processing code for you to call.
for pixel in image: # use pixel in here pixel.red = 0
image = SimpleImage.blank(200, 100)
image = SimpleImage.blank(200, 100, 'black')
image = SimpleImage('', width=200, height=100)
pixel = image.get_pixel(10, 20)
The "foreach" above is the easiest way to loop over all the pixels. However sometimes you want to write loops to access pixels by their x,y coordinates. Here is the canonical nested range loops to access all the pixels in an image. This loop does the top row (y=0), then the next row (y=1) and so on.
def example(filename): image = SimpleImage(filename) for y in range(image.height): fox x in range(image.width): pixel = image.get_pixel(x, y) # do something with pixel pixel.red = pixel.red // 2 return image