Skip to content
Advertisement

Script freezes after completing a ‘while’ loop in a ‘while’ loop (oops)

How can get the RGB values of every pixel in an image and after it gets all the values of the first row?

Script:

image = input("image:")
im = Image.open(image)
pix = im.load()
width, height = im.size
x = 0
y = 0

# For each pixel in the Y
while (y < height):
    # For each pixel in the X
 while (x < width):
     print pix[x,y]
     x = x + 1
y = y + 1

Advertisement

Answer

The way you initialize your x and y values is the problem. X should be initialized back to zero immediately before the second while loop, so that the count starts again for the width of the next row.

Something like:

x = 0
y = 0
#for each pixel in the Y
while (y < height):
    # for each pixel in the X
 x = 0 #start counting again for the next row
 while (x < width):
     print pix[x,y]
     x = x + 1
 y = y + 1

Your loop freezes, because at the end of the first row, x=width and you forget to reset it back to zero for the second iteration of the first while loop.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement