NumPy: Flip array (np.flip, flipud, fliplr)
Using numpy.flip()
you can flip the NumPy array ndarray
vertically (up-down) or horizontally (left-right). There are also numpy.flipud()
specialized for vertical flipping and numpy.fliplr()
specialized for horizontal flipping.
This article describes the following contents.
- Flip
ndarray
vertically:np.flipud()
- Flip
ndarray
horizontally:np.fliplr()
- Flip
ndarray
along any axis:np.flip()
- Default behavior
- Specify the axis to flip:
axis
- Flip image vertically and horizontally
It is also possible to invert with slice ::-1
without using these functions.
Flip ndarray
vertically: np.flipud()
Use numpy.flipud()
to flip ndarray
vertically. ud
means "Up" and "Down".
numpy.flipud()
returns a view. Because a view shares memory with the original array, changing one value changes the other.
import numpy as np
a_2d = np.arange(6).reshape(2, 3)
print(a_2d)
# [[0 1 2]
# [3 4 5]]
a_2d_flipud = np.flipud(a_2d)
print(a_2d_flipud)
# [[3 4 5]
# [0 1 2]]
print(np.shares_memory(a_2d, a_2d_flipud))
# True
If you want to process as separate data, use copy()
.
a_2d_flipud_copy = np.flipud(a_2d).copy()
print(a_2d_flipud_copy)
# [[3 4 5]
# [0 1 2]]
print(np.shares_memory(a_2d, a_2d_flipud_copy))
# False
numpy.flipud()
is equivalent to slice [::-1]
. The array flips along the first axis.
print(a_2d[::-1])
# [[3 4 5]
# [0 1 2]]
The same is true for one-dimensional and multi-dimensional arrays of three or more dimensions, where the array is flipped along the first axis. In the case of one-dimensional array, it is different from the impression of "vertically", so be careful.
a_1d = np.arange(3)
print(a_1d)
# [0 1 2]
print(np.flipud(a_1d))
# [2 1 0]
print(a_1d[::-1])
# [2 1 0]
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(np.flipud(a_3d))
# [[[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]
#
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]]
print(a_3d[::-1])
# [[[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]
#
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]]
If you want to flip the multi-dimensional array along any axis, use numpy.flip()
described later.
Flip ndarray
horizontally: np.fliplr()
Use numpy.fliplr()
to flip ndarray
horizontally. lr
means "Left" and "Right".
numpy.flipud()
returns a view. If you want to process as separate data, you can use copy()
as in the example of numpy.flipud()
.
import numpy as np
a_2d = np.arange(6).reshape(2, 3)
print(a_2d)
# [[0 1 2]
# [3 4 5]]
a_2d_fliplr = np.fliplr(a_2d)
print(a_2d_fliplr)
# [[2 1 0]
# [5 4 3]]
print(np.shares_memory(a_2d, a_2d_fliplr))
# True
numpy.fliplr()
is equivalent to slice [:, ::-1]
. The array flips along the second axis.
print(a_2d[:, ::-1])
# [[2 1 0]
# [5 4 3]]
The same is true for one-dimensional and multi-dimensional arrays of three or more dimensions, where the array is flipped along the second axis. Note that in the case of a one-dimensional array, an error is raised because the second axis does not exist.
a_1d = np.arange(3)
print(a_1d)
# [0 1 2]
# print(np.fliplr(a_1d))
# ValueError: Input must be >= 2-d.
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(np.fliplr(a_3d))
# [[[ 8 9 10 11]
# [ 4 5 6 7]
# [ 0 1 2 3]]
#
# [[20 21 22 23]
# [16 17 18 19]
# [12 13 14 15]]]
print(a_3d[:, ::-1])
# [[[ 8 9 10 11]
# [ 4 5 6 7]
# [ 0 1 2 3]]
#
# [[20 21 22 23]
# [16 17 18 19]
# [12 13 14 15]]]
Flip ndarray along any axis: np.flip()
Use numpy.flip()
to flip ndarray
along any axis. It is also possible to flip along multiple axes.
Default behavior
By default, the array is flipped along all axes. In the case of a two-dimensional array, it flips vertically and horizontally.
numpy.flip()
returns a view. If you want to process as separate data, you can use copy()
as in the example of numpy.flipud()
.
import numpy as np
a_2d = np.arange(6).reshape(2, 3)
print(a_2d)
# [[0 1 2]
# [3 4 5]]
a_2d_flip = np.flip(a_2d)
print(a_2d_flip)
# [[5 4 3]
# [2 1 0]]
print(np.shares_memory(a_2d, a_2d_flip))
# True
This is equivalent to flipping all axes using slices.
print(a_2d[::-1, ::-1])
# [[5 4 3]
# [2 1 0]]
The same is true for one-dimensional and multi-dimensional arrays of three or more dimensions. By default, the array is flipped along all axes.
a_1d = np.arange(3)
print(a_1d)
# [0 1 2]
print(np.flip(a_1d))
# [2 1 0]
print(a_1d[::-1])
# [2 1 0]
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(np.flip(a_3d))
# [[[23 22 21 20]
# [19 18 17 16]
# [15 14 13 12]]
#
# [[11 10 9 8]
# [ 7 6 5 4]
# [ 3 2 1 0]]]
print(a_3d[::-1, ::-1, ::-1])
# [[[23 22 21 20]
# [19 18 17 16]
# [15 14 13 12]]
#
# [[11 10 9 8]
# [ 7 6 5 4]
# [ 3 2 1 0]]]
Specify the axis to flip: axis
The axis to be reversed can be specified by the second argument axis
. axis=0
is equivalent to flipud()
and axis=1
to fliplr()
.
print(np.flip(a_3d, 0))
# [[[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]
#
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]]
print(a_3d[::-1])
# [[[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]
#
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]]
print(np.flipud(a_3d))
# [[[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]
#
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]]
print(np.flip(a_3d, 1))
# [[[ 8 9 10 11]
# [ 4 5 6 7]
# [ 0 1 2 3]]
#
# [[20 21 22 23]
# [16 17 18 19]
# [12 13 14 15]]]
print(a_3d[:, ::-1])
# [[[ 8 9 10 11]
# [ 4 5 6 7]
# [ 0 1 2 3]]
#
# [[20 21 22 23]
# [16 17 18 19]
# [12 13 14 15]]]
print(np.fliplr(a_3d))
# [[[ 8 9 10 11]
# [ 4 5 6 7]
# [ 0 1 2 3]]
#
# [[20 21 22 23]
# [16 17 18 19]
# [12 13 14 15]]]
print(np.flip(a_3d, 2))
# [[[ 3 2 1 0]
# [ 7 6 5 4]
# [11 10 9 8]]
#
# [[15 14 13 12]
# [19 18 17 16]
# [23 22 21 20]]]
print(a_3d[:, :, ::-1])
# [[[ 3 2 1 0]
# [ 7 6 5 4]
# [11 10 9 8]]
#
# [[15 14 13 12]
# [19 18 17 16]
# [23 22 21 20]]]
You can specify multiple axes for axis
using tuple
or list
.
print(np.flip(a_3d, (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(a_3d[:, ::-1, ::-1])
# [[[11 10 9 8]
# [ 7 6 5 4]
# [ 3 2 1 0]]
#
# [[23 22 21 20]
# [19 18 17 16]
# [15 14 13 12]]]
The operation of flip()
is summarized below.
flip(m, 0) is equivalent to flipud(m).
flip(m, 1) is equivalent to fliplr(m).
flip(m, n) corresponds to m[...,::-1,...] with ::-1 at position n.
flip(m) corresponds to m[::-1,::-1,...,::-1] with ::-1 at all positions.
flip(m, (0, 1)) corresponds to m[::-1,::-1,...] with ::-1 at position 0 and position 1.
numpy.flip — NumPy v1.16 Manual
Flip image vertically and horizontally
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)
You can flip the image vertically and horizontally by using numpy.flip()
, numpy.flipud()
, numpy.fliplr()
. The following example uses a color image (three-dimensional array), but the specification of arguments is the same for gray images (two-dimensional array).
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.flipud(img)).save('data/dst/lena_np_flipud.jpg')
Image.fromarray(np.fliplr(img)).save('data/dst/lena_np_fliplr.jpg')
Image.fromarray(np.flip(img, (0, 1))).save('data/dst/lena_np_flip_ud_lr.jpg')
You can also rotate images with OpenCV functions.