How to make a checkerboard in numpy?

后端 未结 24 1344
小鲜肉
小鲜肉 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:18

    I modified hass's answer as follows.

    import math
    import numpy as np
    
    def checkerboard(w, h, c0, c1, blocksize):
            tile = np.array([[c0,c1],[c1,c0]]).repeat(blocksize, axis=0).repeat(blocksize, axis=1)
            grid = np.tile(tile,(int(math.ceil((h+0.0)/(2*blocksize))),int(math.ceil((w+0.0)/(2*blocksize)))))
            return grid[:h,:w]
    

提交回复
热议问题