How to make a checkerboard in numpy?

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

    I'm not sure if this is better than what I had:

    c = numpy.fromfunction(lambda x,y: ((x//sq) + (y//sq)) % 2, (w,h))
    self.chex = numpy.array((w,h,3))
    self.chex[c == 0] = (0xAA, 0xAA, 0xAA)
    self.chex[c == 1] = (0x99, 0x99, 0x99)
    
    0 讨论(0)
  • 2020-11-30 08:09

    import numpy as np
    b = np.array([[0,1],[1,0]])

    Replace n with a even numberand you will get the answer. Easy right?

    np.tile(b,(n, n))

    0 讨论(0)
  • 2020-11-30 08:14
    import numpy as np
    n = int(input())
    arr = ([0, 1], [1,0])
    print(np.tile(arr, (n//2,n//2)))
    

    For input 6, output:

       [[0 1 0 1 0 1]
        [1 0 1 0 1 0]
        [0 1 0 1 0 1]
        [1 0 1 0 1 0]
        [0 1 0 1 0 1]
        [1 0 1 0 1 0]]
    
    0 讨论(0)
  • 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]]
    
    0 讨论(0)
  • 2020-11-30 08:15

    Simplest implementation of the same.

    import numpy as np
    
    n = int(input())
    checkerboard = np.tile(np.array([[0,1],[1,0]]), (n//2, n//2))
    print(checkerboard)
    
    0 讨论(0)
  • 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]]
    
    0 讨论(0)
提交回复
热议问题