Easiest way to flip the dimension of a movie with python

106 Views Asked by At

I have a movie, loaded from a tif file using skimage.external.tifffile.imread() into a numpy array with the shape (frames, width, height). What will be the best way to reorder the movie to the shape ( width, height, frames)?

I can build a function that does this using a for loop, but is there a better way to reshape while avoiding a for loop implementation? Some sort of vectorization of the problem?

2

There are 2 best solutions below

0
BlackBear On BEST ANSWER

You can use numpy.moveaxis:

movie = np.moveaxis(movie, 0, 2)
0
Dani Mesejo On

You can do a transpose followed by a swapaxes:

import numpy as np

movies = np.zeros((10, 250, 100))

print(movies.shape)
print(np.swapaxes(movies.T, 0, 1).shape)

Output

(10, 250, 100)
(250, 100, 10)