在'while'循环中完成'while'循环后脚本冻结(糟糕)



如何获取图像中每个像素的RGB值,以及在获取第一行的所有值之后?

脚本:

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

初始化x和y值的方式是个问题。X应在while循环的第二个循环之前立即初始化回零,以便对下一行的宽度再次开始计数。

类似于:

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

循环冻结,因为在第一行的末尾,x=宽度,而在循环的第一次的第二次迭代中,您忘记将其重置为零。

最新更新