i = 0
for x in range(0, 5):
for y in range(0, 5):
if 0 == outputAfterLearning[i]:
image.putpixel((x, y), (0, 0, 0))
elif 1 == outputAfterLearning[i]:
image.putpixel((x, y), (255, 255, 255))
i += 1
for x in range(0, 5):
for y in range(5, 10):
if 0 == outputAfterLearning[i]:
image.putpixel((x, y), (0, 0, 0))
elif 1 == outputAfterLearning[i]:
image.putpixel((x, y), (255, 255, 255))
i += 1
for x in range(5, 10):
for y in range(0, 5):
if 0 == outputAfterLearning[i]:
image.putpixel((x, y), (0, 0, 0))
elif 1 == outputAfterLearning[i]:
image.putpixel((x, y), (255, 255, 255))
i += 1
for x in range(5, 10):
for y in range(5, 10):
if 0 == outputAfterLearning[i]:
image.putpixel((x, y), (0, 0, 0))
elif 1 == outputAfterLearning[i]:
image.putpixel((x, y), (255, 255, 255))
i += 1
如您所见,我正在使用5x5像素的正方形迭代图像并在其中设置像素。
上述代码显然适用于尺寸为10x10px的图像,但我希望以更通用的方式编写上述代码,这样我就可以在不添加32个新的循环的情况下将其用于更大的图像(比如30x30px)。