我使用numpy将像素数组初始化为灰色棋盘(“无像素”或透明的经典表示)。好像有应该是一个whizzy方式与numpy的惊人阵列分配/切片/切割操作做到这一点,但是这是我想出来的最好的:如何在numpy中制作棋盘格?
w, h = 600, 800
sq = 15 # width of each checker-square
self.pix = numpy.zeros((w, h, 3), dtype=numpy.uint8)
# Make a checkerboard
row = [[(0x99,0x99,0x99),(0xAA,0xAA,0xAA)][(i//sq)%2] for i in range(w)]
self.pix[[i for i in range(h) if (i//sq)%2 == 0]] = row
row = [[(0xAA,0xAA,0xAA),(0x99,0x99,0x99)][(i//sq)%2] for i in range(w)]
self.pix[[i for i in range(h) if (i//sq)%2 == 1]] = row
它的工作原理,但我希望为了更简单一些。
我没有得到任何尊重这里,但这是正确的。 http://telliott99.blogspot.com/2010/01/heres-question-on-so-about-how-to-make.html – telliott99 2010-01-30 23:54:19
这并没有创建一个正确大小的数组,虽然它看起来像你在您的博客文章中扩展您的答案。但是我们不能投票发表博客文章! :) Sheesh! – 2010-01-31 16:18:04