如何在numpy中制作棋盘? [英] How to make a checkerboard in numpy?

查看:243
本文介绍了如何在numpy中制作棋盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用numpy将像素数组初始化为灰色棋盘(无像素"或透明的经典表示形式).似乎应该有一种怪诞的方法来执行numpy惊人的数组分配/切片/切块操作,但这是我想出的最好的方法:

I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:

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

它可以工作,但是我希望有一些简单的方法.

It works, but I was hoping for something simpler.

推荐答案

我会使用此示例中的棋盘格在每个方向上都有2 * 4 = 8个大小为10x10的字段.

The checkerboard in this example has 2*4=8 fields of size 10x10 in each direction.

这篇关于如何在numpy中制作棋盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆