How to make a checkerboard in numpy?

后端 未结 24 1345
小鲜肉
小鲜肉 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条回答
  •  旧时难觅i
    2020-11-30 08:28

    Here is the solution using tile function in numpy.

    import numpy as np
    
    x = np.array([[0, 1], [1, 0]])
    check = np.tile(x, (n//2, n//2))
    # Print the created matrix
    print(check)
    
    1. for input 2, the Output is
         [[0 1]
         [1 0]]
    
    1. for input 4, the Output is
         [[0 1 0 1] 
         [1 0 1 0]
         [0 1 0 1]
         [1 0 1 0]]
    

提交回复
热议问题