How to make a checkerboard in numpy?

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

    Suppose we need a patter with length and breadth (even number) as l, b.

    base_matrix = np.array([[0,1],[1,0]])

    As this base matrix, which would be used as a tile already has length and breadth of 2 X 2, we would need to divide by 2.

    print np.tile(base_matrix, (l / 2, b / 2))

    print (np.tile(base,(4/2,6/2)))
    [[0 1 0 1 0 1]
     [1 0 1 0 1 0]
     [0 1 0 1 0 1]
     [1 0 1 0 1 0]]
    

提交回复
热议问题