Search code examples
pythonimagedimensions

Transpose image dimensions


Hy! Is there a solution for changing the (width, height, channel) dimensions of an image to (channel, height, width) in Python?

For example: 224 x 224 x 3 -> 3 x 224 x 224


Solution

  • You can use np.transpose https://numpy.org/doc/1.18/reference/generated/numpy.transpose.html:

    new_image = np.transpose(image, (2, 0, 1))