我正在Python中编写一个函数来使图像像素化,但由于某些原因,xx和x不会增加。两个变量始终保持值0,并没有任何反应。Python嵌套while循环不会按预期增加
xx = 0
yy = 0
while xx < width:
while yy < height:
print ("---", xx, yy)
offsetX = xx + block_size_half
offsetY = yy + block_size_half
while (xx + offsetX >= width):
offsetX -= 1
while (yy + offsetY >= height):
offsetY -= 1
r, g, b = matrix[xx + offsetX][yy + offsetY]
x = xx
y = yy
while x < xx + block_size and x < width:
while y < yy + block_size and y < height:
print (x, y)
matrix[x][y] = (r, g, b)
y += 1
x += 1
yy += block_size
xx += block_size
感谢@Elazar的帮助!这里是整个工作的功能:
def pixelate(matrix, block_size):
width = len(matrix)
height = len(matrix[0])
block_size_half = int(block_size/2)
for xx in range(0, width, block_size):
for yy in range(0, height, block_size):
offsetX = min(xx + block_size_half, width - 1)
offsetY = min(yy + block_size_half, height - 1)
r, g, b = matrix[offsetX][offsetY]
for x in range(xx, min(xx + block_size, width)):
for y in range(yy, min(yy + block_size, height)):
matrix[x][y] = (r, g, b)
我的灵感来自这个C#实现的算法: http://notes.ericwillis.com/2009/11/pixelate-an-image-with-csharp/
我不确定,但检查缩进问题 – 2013-04-26 05:17:02
也许'block_size == 0'? – Elazar 2013-04-26 05:19:40
GrijeshChauhan:我试图看看缩进是否有问题,但找不到任何东西。 Elazar:不应该是问题,因为yy和y正确递增。感谢下面的答案! – user2252177 2013-04-26 06:44:35