How to “scale” a numpy array?

后端 未结 4 2078
天涯浪人
天涯浪人 2020-12-08 07:24

I would like to scale an array of shape (h, w) by a factor of n, resulting in an array of shape (h*n, w*n), with the.

Say that I have a 2x2 array:

ar         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 07:48

    scipy.misc.imresize can scale images. It can be used to scale numpy arrays, too:

    #!/usr/bin/env python
    
    import numpy as np
    import scipy.misc
    
    def scale_array(x, new_size):
        min_el = np.min(x)
        max_el = np.max(x)
        y = scipy.misc.imresize(x, new_size, mode='L', interp='nearest')
        y = y / 255 * (max_el - min_el) + min_el
        return y
    
    x = np.array([[1, 1],
                  [0, 1]])
    n = 2
    new_size = n * np.array(x.shape)
    y = scale_array(x, new_size)
    print(y)
    

提交回复
热议问题