Search code examples
imageloopsfor-loopjythonpixel

loop (for loop) though even pixels in a picture in jython


I need to use a single for loop to loop through every even pixel in a picture. I think I was getting close with this code, but jython does not like it and I do not know why (something with the second for loop).

for x in range(0, width):
  for y in range(0, height):
    px = getPixels(pic, x, y)

Any help would be much appreciated.

Here is my full code if it helps. The point of the project is to resize a picture by moving all the even pixels to a new blank picture that is half the size.

def main():
  #Allows the user to pick a picture
    pic = makePicture(pickAFile())
    show(pic)
    #Finds the width and height of the selected picture
    width = getWidth(pic)
    height = getHeight(pic)

  #Finds and divides width accordingly
    if width % 2 == 0:
      newW = width/2
    else:
      newW = width/2+1

  #Finds and divides height accordingly  
    if height % 2 == 0:
      newH = height/2
    else:
      newH = height/2+1

for x in range(0, width, 2):
  for y in range(0, height, 2):
    px = getPixels(pic, x, y)

Solution

  • Is it an issue with not tabbing over the loop for the y values? Does structuring the text like this fix the issue? (By the way, adding a 3rd parameter to the range() function lets you define a step value instead of the default 1.)

    for x in range(0, width, 2):
        for y in range(0, height, 2):
            px = getPixels(pic, x, y)