Quick way to upsample numpy array by nearest neighbor tiling [duplicate]

One option is

>>> a.repeat(2, axis=0).repeat(2, axis=1)
array([[0, 0, 1, 1, 2, 2],
       [0, 0, 1, 1, 2, 2],
       [3, 3, 4, 4, 5, 5],
       [3, 3, 4, 4, 5, 5],
       [6, 6, 7, 7, 8, 8],
       [6, 6, 7, 7, 8, 8]])

This is slightly wasteful due to the intermediate array but it’s concise at least.

Leave a Comment