Search code examples
pythonartificial-intelligence

Can't reshape .npy array from 1D raw patches to 2D image patches


Need to reshape 1-d 2914 pxls per picture array to 2-d 62*47 array, from a dataset

tried to creative a patches variable with np.load attached and then using np.shape(62,47) on said variable, got a "tuple isnt callable error"


Solution

  • np.shape is not a function. It returns the shape of your array, which is a tuple. You need to use the np.reshape function.

    import numpy as np
    
    pic = np.random.rand(2914)
    print(f"Shape: {pic.shape}")
    
    pic = np.reshape(pic, (62, 47))
    print(f"Shape: {pic.shape}")
    
    print(f"np.shape: {np.shape(pic)}")
    print(f"type of np.shape: {type(np.shape(pic))}")