How to make a checkerboard in numpy?

后端 未结 24 1327
小鲜肉
小鲜肉 2020-11-30 07:52

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

24条回答
  •  情话喂你
    2020-11-30 08:28

    def checkerboard(shape):
        return np.indices(shape).sum(axis=0) % 2
    

    Most compact, probably the fastest, and also the only solution posted that generalizes to n-dimensions.

提交回复
热议问题