Skip to content
Advertisement

Expanding/Zooming in a numpy array

I have the following array:

import numpy as np
a = np.array([[2, 3, 5],
              [4, 6, 7],
              [1, 5, 7]])

I want to expand it to this array:

b = [[2 2 2 3 3 3 5 5 5]
     [2 2 2 3 3 3 5 5 5]
     [2 2 2 3 3 3 5 5 5]
     [4 4 4 6 6 6 7 7 7]
     [4 4 4 6 6 6 7 7 7]
     [4 4 4 6 6 6 7 7 7]
     [1 1 1 5 5 5 7 7 7]
     [1 1 1 5 5 5 7 7 7]
     [1 1 1 5 5 5 7 7 7]]

So I’m using the following command:

import scipy.ndimage
b = scipy.ndimage.interpolation.zoom(a, 3, order=0)

based on this question and answer here Resampling a numpy array representing an image.

However, what I’m getting is this:

b = [[2 2 3 3 3 3 5 5 5]
     [2 2 3 3 3 3 5 5 5]
     [4 4 6 6 6 6 7 7 7]
     [4 4 6 6 6 6 7 7 7]
     [4 4 6 6 6 6 7 7 7]
     [4 4 6 6 6 6 7 7 7]
     [1 1 5 5 5 5 7 7 7]
     [1 1 5 5 5 5 7 7 7]
     [1 1 5 5 5 5 7 7 7]]

I want the expansion to be exactly by 3, or whatever the zoom factor is, but currently it’s different for each element of the array.

Is there a direct way to do this? Or shall I do it manually with some coding?

Advertisement

Answer

Maybe a little late, but for the sake of completness: Numpy Kron does the job perfectly

>>> import numpy as np
>>> a = np.array([[2,3,5], [4,6,7], [1,5,7]])
>>> np.kron(a, np.ones((3,3)))
array([[ 2.,  2.,  2.,  3.,  3.,  3.,  5.,  5.,  5.],
       [ 2.,  2.,  2.,  3.,  3.,  3.,  5.,  5.,  5.],
       [ 2.,  2.,  2.,  3.,  3.,  3.,  5.,  5.,  5.],
       [ 4.,  4.,  4.,  6.,  6.,  6.,  7.,  7.,  7.],
       [ 4.,  4.,  4.,  6.,  6.,  6.,  7.,  7.,  7.],
       [ 4.,  4.,  4.,  6.,  6.,  6.,  7.,  7.,  7.],
       [ 1.,  1.,  1.,  5.,  5.,  5.,  7.,  7.,  7.],
       [ 1.,  1.,  1.,  5.,  5.,  5.,  7.,  7.,  7.],
       [ 1.,  1.,  1.,  5.,  5.,  5.,  7.,  7.,  7.]])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement