NumPy: Arrange ndarray in tiles with np.tile()
By the NumPy function np.tile()
, you can generate a new ndarray
in which the original ndarray
is repeatedly arranged like tiles.
This article describes the following contents.
- Basic usage of
np.tile()
- For two-dimensional (multi-dimensional) array
- Image processing: Arrange the image in tiles
Basic usage of np.tile()
Take a one-dimensional array as an example.
import numpy as np
a = np.arange(3)
print(a)
# [0 1 2]
Pass the original ndarray
in the first argument A
and the number to repeat in the second argument reps
. A new array is returned, and the original array itself remains unchanged.
a_tile = np.tile(a, 3)
print(a_tile)
# [0 1 2 0 1 2 0 1 2]
print(a)
# [0 1 2]
By passing a tuple to the second argument reps
, the original array is repeated for each axis.
For example, if reps
is (2, 3)
, the original array will be repeated 2
times for axis=0
and 3
times for axis=1
, and the result will be a two-dimensional array.
print(np.tile(a, (2, 3)))
# [[0 1 2 0 1 2 0 1 2]
# [0 1 2 0 1 2 0 1 2]]
print(np.tile(a, (2, 3)).shape)
# (2, 9)
If the number of tuple elements is 3
, a three-dimensional array is returned.
print(np.tile(a, (2, 3, 4)))
# [[[0 1 2 0 1 2 0 1 2 0 1 2]
# [0 1 2 0 1 2 0 1 2 0 1 2]
# [0 1 2 0 1 2 0 1 2 0 1 2]]
#
# [[0 1 2 0 1 2 0 1 2 0 1 2]
# [0 1 2 0 1 2 0 1 2 0 1 2]
# [0 1 2 0 1 2 0 1 2 0 1 2]]]
print(np.tile(a, (2, 3, 4)).shape)
# (2, 3, 12)
For two-dimensional (multi-dimensional) array
Take a two-dimensional array as an example.
a_2d = np.arange(6).reshape(2, 3)
print(a_2d)
# [[0 1 2]
# [3 4 5]]
If you specify the second argument reps
as an integer or with a tuple with fewer elements than the number of dimensions of the original array (2
for a two-dimensional array), the original array is repeated along the axis of the last dimension.
For example, if reps=2
or reps=(2,)
(= a tuple with one element), the result will be as follows:
print(np.tile(a_2d, 2))
# [[0 1 2 0 1 2]
# [3 4 5 3 4 5]]
print(np.tile(a_2d, (2, )))
# [[0 1 2 0 1 2]
# [3 4 5 3 4 5]]
This is equivalent to reps=(1, 2)
and is repeated 2
times for axis=1
.
print(np.tile(a_2d, (1, 2)))
# [[0 1 2 0 1 2]
# [3 4 5 3 4 5]]
If you want to repeat only for axis=0
, set reps=(2, 1)
.
print(np.tile(a_2d, (2, 1)))
# [[0 1 2]
# [3 4 5]
# [0 1 2]
# [3 4 5]]
It is possible to add more dimensions than the original array.
print(np.tile(a_2d, (2, 2, 2)))
# [[[0 1 2 0 1 2]
# [3 4 5 3 4 5]
# [0 1 2 0 1 2]
# [3 4 5 3 4 5]]
#
# [[0 1 2 0 1 2]
# [3 4 5 3 4 5]
# [0 1 2 0 1 2]
# [3 4 5 3 4 5]]]
print(np.tile(a_2d, (2, 2, 2)).shape)
# (2, 4, 6)
For image processing (Arrange the image in tiles)
By using np.tile()
, the images read as the NumPy array numpy.ndarray
can be repeatedly arranged in tiles.
See the following article for the basics of image processing using NumPy, such as loading and saving images.
This sample code below uses Pillow to load the image, but you can use other libraries such as OpenCV.
In the case of a color image, the shape
of the read ndarray
is (height, width, number of colors)
.
To repeat this ndarray
with np.tile()
, set the second argument reps
to (vertical repetition number, horizontal repetition number, 1)
.
Note that if there is no trailing 1
, that is, (vertical repetition number, horizontal repetition number)
, it will be regarded as (1, vertical repetition number, horizontal repetition number)
, and it will not work.
import numpy as np
from PIL import Image
img = np.array(Image.open('data/src/lena_square.png').resize((128, 128)))
print(img.shape)
# (128, 128, 3)
img_tile = np.tile(img, (2, 3, 1))
print(img_tile.shape)
# (256, 384, 3)
Image.fromarray(img_tile).save('data/dst/lena_numpy_tile.jpg')
Original image. In the sample code above, it is resized when loaded.
Arranged in tiles:
With np.tile()
, you can only arrange the same image. If you want to arrange different images in tiles, see the following articles.
- Concatenate images with Python, Pillow
- Concatenate images with Python, OpenCV (hconcat, vconcat, np.tile)
Another use case for np.tile()
in image processing is the generation of gradation image.