xdim, ydim = 10, 10
xblocksize, yblocksize = 5, 5
for xblock in range(0, xdim, xblocksize):
for yblock in range(0, ydim, yblocksize):
for x in range(xblock, xblock+xblocksize):
for y in range(yblock, yblock+yblocksize):
# the common code.
但我会为块迭代创建一个生成器:
def blocked(xdim, ydim, xblocksize, yblocksize):
for xblock in range(0, xdim, xblocksize):
for yblock in range(0, ydim, yblocksize):
for x in range(xblock, xblock+xblocksize):
for y in range(yblock, yblock+yblocksize):
yield (x, y)
使用putpoixel作为
color = [(0,0,0),(255,255,255)]
for colorcode, pixelloc in zip(outputAfterLearning, blocked(10, 10, 5, 5)):
if 0 <= colorcode < len(color):
# ^ omit this if outputAfterLearning[i] is always valid
image.putpixel(pixelloc, color[colorcode])