How to make a checkerboard in numpy?

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

    Numpy's Tile function to get checkerboard array of size n*n

    import numpy
    
    n = 4
    
    list_0_1 = [ [ 0, 1], [ 1, 0] ]
    
    checkerboard = numpy.tile( list_0_1, ( n//2, n//2) ) 
    
    print( checkerboard)
    
    [[0 1 0 1]
     [1 0 1 0]
     [0 1 0 1]
     [1 0 1 0]]
    

提交回复
热议问题