Home > Enterprise >  How to fix the shape of the numpy array?
How to fix the shape of the numpy array?

Time:08-10

I have converted the image into a NumPy array. Then uses np.rot90 to rotate the array to 90 degrees.

img90 = np.array(np.rot90(image))
img90.shape

The output I receive is --> (256, 4, 256) But I want it to be (4, 256, 256)

I have tried

img90 = np.rollaxis(img90, 1, 0)
img90.shape

But the output is not fixed, sometimes it gives (256, 4,256) and sometimes (4, 256, 256)

CodePudding user response:

You should specify the axes axes and number of rotation k parameters

assuming your array size is 256x256x4:

img0 = np.rot90(img0, axes=(0,2), k= 1)
print(img0.shape)
>>> (4, 256, 256)

CodePudding user response:

if you want to change your image shape you can do this:

nsamples, nx, ny = input_img.shape
New_Shape = input_img.reshape((nsamples,nx*ny))

or

nsamples, nx, ny = (4, 256, 256)
New_Shape = input_img.reshape((nsamples,nx*ny))

Where the nsamples, nx, ny is the new image shape you desire, and then use reshape to reshape your image as you like.

  • Related