Active February 06, 2022    /    Viewed 265    /    Comments 0    /    Edit

Examples of how to pad a numpy array in python:

### Create a 2D array with numpy

Let's consider the following array:

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

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)


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

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]]


 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]]


 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]]


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()


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()


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()


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()


### References

##### Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!

Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount