How to pad a numpy array in python ?

Published: February 06, 2022

Updated: December 09, 2022

Tags: Python; Numpy;

DMCA.com Protection Status

Examples of how to pad a numpy array in python:

Create a 2D array with numpy

Let's consider the following array:

\begin{equation}
A = \left( \begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right)
\end{equation}

that can be implemented using numpy and python:

import numpy as np

A = np.arange(0,12)

A = A.reshape(4,3)

print(A)

gives

[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

and

print( A.shape )

gives

(4, 3)

Pad a numpy array

To pad a numpy array in python, a solution is to use numpy.pad:

Pad with constant_values

Add a (1,1) pad with the value -99

B = np.pad(A, (1, 1), constant_values=-99)

gives

[[-99 -99 -99 -99 -99]
 [-99   0   1   2 -99]
 [-99   3   4   5 -99]
 [-99   6   7   8 -99]
 [-99   9  10  11 -99]
 [-99 -99 -99 -99 -99]]

Add a (3,3) pad with the value -99

 B = np.pad(A, (3, 3), constant_values=-99)

gives

[[-99 -99 -99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99   0   1   2 -99 -99 -99]
 [-99 -99 -99   3   4   5 -99 -99 -99]
 [-99 -99 -99   6   7   8 -99 -99 -99]
 [-99 -99 -99   9  10  11 -99 -99 -99]
 [-99 -99 -99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99 -99 -99 -99 -99 -99 -99]]

Add a (3,1) pad with the value -99

 B = np.pad(A, (3, 1), constant_values=-99)

gives

[[-99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99 -99 -99 -99 -99]
 [-99 -99 -99   0   1   2 -99]
 [-99 -99 -99   3   4   5 -99]
 [-99 -99 -99   6   7   8 -99]
 [-99 -99 -99   9  10  11 -99]
 [-99 -99 -99 -99 -99 -99 -99]]

Pad with neareast value

Add a (1,1) pad with neareast value

B = np.pad(A, (1, 1), 'edge')

gives

    [[ 0  0  1  2  2]
     [ 0  0  1  2  2]
     [ 3  3  4  5  5]
     [ 6  6  7  8  8]
     [ 9  9 10 11 11]
     [ 9  9 10 11 11]]

and

print(B.shape)

gives

(6, 5)

Examples

Find pixel neighbourhood

Example of how to find the

B = np.pad(A, (1, 1), 'edge')

then sliced the matrix B to find the 8 neighbourhood pixels"

Right side

B[0:-2,0:-2]

B[1:-1,0:-2]

B[2:,0:-2]

gives

[[0 0 1]
 [0 0 1]
 [3 3 4]
 [6 6 7]]

[[ 0  0  1]
 [ 3  3  4]
 [ 6  6  7]
 [ 9  9 10]]

[[ 3  3  4]
 [ 6  6  7]
 [ 9  9 10]
 [ 9  9 10]]

Middle Top and Bottom:

B[0:-2,1:-1]

B[2:,1:-1]

gives

[[0 1 2]
 [0 1 2]
 [3 4 5]
 [6 7 8]]

[[ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [ 9 10 11]]

Left side

B[0:-2,2:] 
B[1:-1,2:] 
B[2:,2:]

gives

[[1 2 2]
 [1 2 2]
 [4 5 5]
 [7 8 8]]

[[ 1  2  2]
 [ 4  5  5]
 [ 7  8  8]
 [10 11 11]]

[[ 4  5  5]
 [ 7  8  8]
 [10 11 11]
 [10 11 11]]

Add a frame to an image

from matplotlib import image

import matplotlib.pyplot as plt

img = image.imread("eiffel-tower.jpeg")

plt.imshow(img)

plt.show()

print(img.shape)

img1 = np.pad(img, ((100, 100), (200, 200), (0,0)), constant_values=0)

print(img1.shape)

plt.imshow(img1)

plt.savefig("pad_image_01.png", bbox_inches='tight', dpi=100)

plt.show()

Add a frame to an image
Add a frame to an image

img1 = np.pad(img, ((100, 100), (0, 0), (0,0)), constant_values=0)

print(img1.shape)

plt.imshow(img1)

plt.savefig("pad_image_02.png", bbox_inches='tight', dpi=100)

plt.show()

Add a frame to an image
Add a frame to an image

img1 = np.pad(img, ((0, 0), (200, 200), (0,0)), constant_values=0)

print(img1.shape)

plt.imshow(img1)

plt.savefig("pad_image_03.png", bbox_inches='tight', dpi=100)

plt.show()

Add a frame to an image
Add a frame to an image

img1 = np.pad(img, ((100, 100), (0, 0), (0,0)), 'edge')

print(img1.shape)

plt.imshow(img1)

plt.savefig("pad_image_04.png", bbox_inches='tight', dpi=100)

plt.show()

Add a frame to an image
Add a frame to an image

Image

of