NumPy: Rotate array (np.rot90)
Using numpy.rot90()
, you can rotate the NumPy array ndarray
by 90/180/270 degrees.
This article describes the following contents.
- Basic usage of
numpy.rot90()
- Default behavior
- Specify the number of times to rotate:
k
- For one-dimensional array
- For multi-dimensional array
- Default behavior
- Specify the plane to rotate:
axes
- Rotate image
If you want to transpose instead of rotate, see the following article.
Basic usage of numpy.rot90()
Default behavior
Specify ndarray
to be rotated as the first argument of numpy.rot90()
. The array rotates 90 degrees counterclockwise.
import numpy as np
a_2d = np.arange(6).reshape(2, 3)
print(a_2d)
# [[0 1 2]
# [3 4 5]]
a_2d_rot = np.rot90(a_2d)
print(a_2d_rot)
# [[2 5]
# [1 4]
# [0 3]]
numpy.rot90()
returns a view. Because a view shares memory with the original array, changing one value changes the other.
print(np.shares_memory(a_2d, a_2d_rot))
# True
a_2d_rot[0, 0] = 100
print(a_2d_rot)
# [[100 5]
# [ 1 4]
# [ 0 3]]
print(a_2d)
# [[ 0 1 100]
# [ 3 4 5]]
a_2d[0, 2] = 2
print(a_2d)
# [[0 1 2]
# [3 4 5]]
print(a_2d_rot)
# [[2 5]
# [1 4]
# [0 3]]
If you want to process as separate data, use copy()
.
a_2d_rot_copy = np.rot90(a_2d).copy()
print(a_2d_rot_copy)
# [[2 5]
# [1 4]
# [0 3]]
print(np.shares_memory(a_2d, a_2d_rot_copy))
# False
Specify the number of times to rotate: k
Specifying an integer value for the second argument k
rotates the array 90 degrees counterclockwise k
times.
print(np.rot90(a_2d, 2))
# [[5 4 3]
# [2 1 0]]
print(np.rot90(a_2d, 3))
# [[3 0]
# [4 1]
# [5 2]]
print(np.rot90(a_2d, 4))
# [[0 1 2]
# [3 4 5]]
print(np.rot90(a_2d, 100))
# [[0 1 2]
# [3 4 5]]
In the case of a negative value, the direction of rotation is clockwise.
print(np.rot90(a_2d, -1))
# [[3 0]
# [4 1]
# [5 2]]
print(np.rot90(a_2d, -2))
# [[5 4 3]
# [2 1 0]]
For one-dimensional array
One-dimensional arrays cannot be rotated.
a_1d = np.arange(3)
print(a_1d)
# [0 1 2]
# print(np.rot90(a_1d))
# ValueError: Axes must be different.
It can be rotated if defined as a two-dimensional array of only one row.
a_2d_row = np.arange(3).reshape(1, 3)
print(a_2d_row)
# [[0 1 2]]
print(np.rot90(a_2d_row))
# [[2]
# [1]
# [0]]
For multi-dimensional array
Multi-dimensional arrays of three or more dimensions can also be rotated.
Default behavior
By default, it is rotated as follows.
a_3d = np.arange(24).reshape(2, 3, 4)
print(a_3d)
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
print(a_3d.shape)
# (2, 3, 4)
print(np.rot90(a_3d))
# [[[ 8 9 10 11]
# [20 21 22 23]]
#
# [[ 4 5 6 7]
# [16 17 18 19]]
#
# [[ 0 1 2 3]
# [12 13 14 15]]]
print(np.rot90(a_3d).shape)
# (3, 2, 4)
It is rotated in a plane consisting of the first two axes (dimensions).
print(a_3d[:, :, 0])
# [[ 0 4 8]
# [12 16 20]]
print(np.rot90(a_3d)[:, :, 0])
# [[ 8 20]
# [ 4 16]
# [ 0 12]]
Specify the plane to rotate: axes
For multi-dimensional arrays, the third argument axes
can specify a plane to rotate. In axes
, specify two axes that make up a plane with a tuple or list with two elements.
The default is axes=(0, 1)
, which rotates in the plane of the first two axes. It can be confirmed that the result is the same as the above example.
print(np.rot90(a_3d, axes=(0, 1)))
# [[[ 8 9 10 11]
# [20 21 22 23]]
#
# [[ 4 5 6 7]
# [16 17 18 19]]
#
# [[ 0 1 2 3]
# [12 13 14 15]]]
An example of rotating in another plane is as follows. If the order of the axes specified in axes
is reversed, the direction of rotation is reversed.
print(np.rot90(a_3d, axes=(1, 2)))
# [[[ 3 7 11]
# [ 2 6 10]
# [ 1 5 9]
# [ 0 4 8]]
#
# [[15 19 23]
# [14 18 22]
# [13 17 21]
# [12 16 20]]]
print(np.rot90(a_3d, axes=(1, 2)).shape)
# (2, 4, 3)
print(np.rot90(a_3d, axes=(2, 1)))
# [[[ 8 4 0]
# [ 9 5 1]
# [10 6 2]
# [11 7 3]]
#
# [[20 16 12]
# [21 17 13]
# [22 18 14]
# [23 19 15]]]
print(np.rot90(a_3d, axes=(2, 1)).shape)
# (2, 4, 3)
It can also be specified together with the second argument k
.
print(np.rot90(a_3d, 2, axes=(1, 2)))
# [[[11 10 9 8]
# [ 7 6 5 4]
# [ 3 2 1 0]]
#
# [[23 22 21 20]
# [19 18 17 16]
# [15 14 13 12]]]
print(np.rot90(a_3d, -1, axes=(1, 2)))
# [[[ 8 4 0]
# [ 9 5 1]
# [10 6 2]
# [11 7 3]]
#
# [[20 16 12]
# [21 17 13]
# [22 18 14]
# [23 19 15]]]
Rotate image
Image files can be read as NumPy array ndarray
using libraries such as Pillow (PIL) and OpenCV.
- Image processing with Python, NumPy
- Reading and saving image files with Python, OpenCV (imread, imwrite)
Images can be rotated using numpy.rot90()
. The following example uses a color image (three-dimensional array), but a gray image (two-dimensional array) also does not need to specify any arguments.
The following image is used as an example.
Sample code and results are below.
import numpy as np
from PIL import Image
img = np.array(Image.open('data/src/lena.jpg'))
print(type(img))
# <class 'numpy.ndarray'>
print(img.shape)
# (225, 400, 3)
Image.fromarray(np.rot90(img)).save('data/dst/lena_np_rot90.jpg')
Image.fromarray(np.rot90(img, 2)).save('data/dst/lena_np_rot90_180.jpg')
Image.fromarray(np.rot90(img, 3)).save('data/dst/lena_np_rot90_270.jpg')
You can also rotate images with OpenCV functions.