"
This article is part of in the series

python codes on computer screen

The NumPy module in Python’s standard library comes loaded with functions that save developers a lot of time and effort. The NumPy pad() function comes especially handy in deep learning and is most helpful in developing convolutional neural networks.

But at the basic level, the applications of this function include adding a pad of numbers to arrays and matrices. You can also use the function with image files to add borders or apply various effects to them.

Mastering this function is one of the first steps to building deep learning models to process and classify images. 

In this article, we will walk you through the syntax and workings of the numpy’s pad() function with examples.

Basics of NumPy Pad()

The pad() function requires two parameters – array and pad_width – to work. However, it accepts several other parameters that modify its functioning to suit your needs.

So, the basic syntax of the function is:

np.pad(

   array, 

   pad_width

)

 

The “array” parameter must be set to the array you want to pad. As you might have guessed, the pad_width parameter expects an integer value denoting how many values to pad on each axis.

Bear in mind that the function returns a new array. However, unlike the original array, this array is padded on the borders. 

The other arguments you can supply to this function include:

Parameter Description Expected Values Default Values
mode= Expects the type of padding to apply. Function or string ‘constant’
stat_length= When mode is set to maximum, minimum, mean, or median, using this parameter calculates a statistic by counting the number of values at the edge of each axis. Integer or sequence ‘None’
constant_values= When the mode parameter is set to its default value, this parameter may be used to change the number the array is padded with. Scalar or sequence 0
end_values= Expects the end value of the linear ramp. Only works when mode is set to linear-ramp.  Scalar or sequence 0
reflect_type= It is used when mode is set to symmetric or reflect. Odd or even values N/A
**kwargs Additional keyword arguments can be used. N/A N/A

 

Interestingly, only the mode parameter is used as frequently as the two non-optional parameters themselves. This is because the mode parameter gives developers a lot of flexibility. 

As mentioned earlier, the mode parameter allows us to alter the type of padding applied to an array. Some of the arguments you can pass to the mode parameter include:

Argument Effect on Padding
constant Padding is a constant value.
edge Padding is the array’s edge values.
empty  Padding becomes undefined values.
linear-ramp Padding is the linear ramp between the end_value and the edge value of the array.
maximum  Padding is the maximum value of either a part or the entirety of a vector along every axis.
mean  Padding is the mean value of a part or entirety of the vector along every axis.
median Padding is the median value of a part or entirety of the vector along every axis.
minimum  Padding is the minimum value of a part or entirety of the vector along every axis.
reflect Padding is the reflection of the vector.
wrap Padding is the vector’s wrap along the axis.
symmetric Padding is the reflection of the vector, which is mirrored along the array’s edge.

 

With an understanding of all the parameters and arguments that work with numpy’s pad() function, you’re ready to see the function in action.

Padding a Simple Array with the pad() Function

Working with a one-dimensional array is an excellent starting point for learning how the pad() function works. 

import numpy as np
original_array = np.array([4, 5, 6, 7, 8])
pad_width = 2  # Amount of padding to add on both sides
# Use np.pad() to add padding to the one-dimensional array
padded_array = np.pad(original_array, pad_width, mode='constant', constant_values=0)
print(padded_array)

 

Here, we have an original one-dimensional array called original_array with values [4, 5, 6, 7, 8]. We want to add 2 zeros on both sides of the array. 

The np.pad() function is used with the pad_width parameter set to 2, indicating the number of elements to add on each side. 

The mode='constant' argument tells np.pad() to pad with a constant value, and constant_values=0 specifies that the padding should be filled with zeros.

The output of the code is:

[0 0 4 5 6 7 8 0 0]
 

It’s worth noting that you can set up different volumes of padding on either side of a one-dimensional array. 

It’s as simple as presenting the pad_width argument as a tuple. This way, each value will represent the padding width on the left and right sides of the supplied array, respectively.

Here’s what this looks like:

import numpy as np

original_array = np.array([4, 5, 6, 7, 8])

# Use np.pad() to add padding to the one-dimensional array

padded_array = np.pad(original_array, (3, 2), mode='constant', constant_values=0)

print(padded_array)

 

The output is:

[0 0 0 4 5 6 7 8 0 0]
 

Of course, you can also change the value of the number you want to pad the array with by changing the constant_values argument, like so:

import numpy as np
original_array = np.array([4, 5, 6, 7, 8])
# Use np.pad() to add padding to the one-dimensional array
padded_array = np.pad(original_array, (3, 2), mode='constant', constant_values=100)
print(padded_array)

 

Which outputs:

[100 100 100   4   5   6   7   8 100 100]
 

While pad() has some interesting applications in one-dimensional arrays, its capability to work with multi-dimensional arrays is more impressive. 

Padding a Multi-dimensional Array with the pad() Function

The pad() function allows you to specify the amount and value of padding for each dimension of the array. Let’s look at an example:

import numpy as np
original_array = np.array([[2, 4, 6],
                           [8, 10, 12],
                           [14, 16, 18]])
# Define the padding for each dimension
pad_width = ((1, 1), (2, 2))
# Use np.pad() to add padding to the multi-dimensional array
padded_array = np.pad(original_array, pad_width, mode='constant', constant_values=0)
print(padded_array)

 

In this example, we have an original 3x3 two-dimensional array original_array. We want to add padding to it, where we specify different padding amounts for rows and columns. 

pad_width is set as ((1, 1), (2, 2)), indicating that we want to add 1 row of zeros on the top, 1 row of zeros on the bottom, and 2 columns of zeros on the left and 2 columns of zeros on the right.

The output will be:

[[ 0  0  0  0  0  0  0]
 [ 0  0  2  4  6  0  0]
 [ 0  0  8 10 12  0  0]
 [ 0  0 14 16 18  0  0]
 [ 0  0  0  0  0  0  0]]

 

As you can see, the pad() function can also accommodate varied amounts of padding in multi-dimensional arrays.  

Padding a Multi-dimensional array with Nearest Values

The pad() function can pad an array with its edge values. Doing this involves setting “mode” to ‘edge.’ Let’s see this in action:

import numpy as np
original_array = np.array([[3, 7, 8],
                           [2, 5, 1],
                           [9, 4, 6]])

# Define the padding width
pad_width = 1
# Use np.pad() to pad the array with its edge values
padded_array = np.pad(original_array, pad_width, mode='edge')
print(padded_array)

 

In this example, we have an original 3x3 two-dimensional array original_array. We want to pad it using the 'edge' mode with a pad width of 1. 

The 'edge' mode pads the array with values from its edges. In this case, the values on the outermost edges of the original array will be repeated to create the padding.

The output will be:

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

 

This type of padding is sometimes helpful when padding a tensor for convolutional neural networks.

Adding a Border to an Image with the pad() Function

For Python, it’s easy to represent images as arrays. This allows us to use the pad() function to add a solid color border to any image. Here’s how it works:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import image
# Load an image
image_path = 'path_to_your_image.jpg'  # Replace with your image file path
original_image = image.imread(image_path)
# Define the border width in pixels for each dimension
border_width = (100, 100)  # 100 pixels of border width on the top and bottom
border_height = (100, 100)  # 100 pixels of border width on the left and right
# Define the color of the border
border_color = [255, 0, 0]  # Red color in RGB format
# Create a solid color border
border = np.full((original_image.shape[0] + sum(border_width), original_image.shape[1] + sum(border_height), 3), border_color, dtype=np.uint8)
# Insert the original image into the center of the border
border[border_width[0]:-border_width[1], border_height[0]:-border_height[1]] = original_image
# Display the image with the border
plt.imshow(border)
plt.show()

 

In this example, we load an image from a file and define the desired border width on both the top and bottom, as well as the left and right sides. We also specify the color for the border (red in this case).

We create a solid color border using np.full() with the specified color and dimensions. Then, we insert the original image into the center of the border. Finally, we display the image with the added border using Matplotlib.

Make sure to replace 'path_to_your_image.jpg' with the actual file path of the image you want to use. This code will add a red border to the image, but you can change the border_color variable to achieve a different color.

Mirroring an Image with the pad() Function

Another cool effect you can accomplish with pad() is the reflection (mirror) effect. Here’s how it’s done:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import image
# Load an image
image_path = 'path_to_your_image.jpg'  # Replace with your image file path
original_image = image.imread(image_path)
# Get the height and width of the original image
height, width, _ = original_image.shape
# Mirror the image horizontally using np.pad() with 'reflect' mode
mirrored_image = np.pad(original_image, ((0, 0), (width, width), (0, 0)), mode='reflect')
# Display the mirrored image
plt.imshow(mirrored_image)
plt.show()

 

In this example, we load an image from a file, retrieve its height and width, and then use np.pad() to mirror it horizontally. 

We achieve horizontal mirroring by adding a mirrored copy of the image to its right side. The 'reflect' mode in np.pad() ensures that the mirrored portion is created by reflecting the original content.

Replace 'path_to_your_image.jpg' with the actual file path of the image you want to use, and this code will display the mirrored version of the image.