How to add a border around a NumPy array?


Adding a border around a NumPy array can be a useful operation in various applications, such as image processing or data visualization. NumPy is a popular Python library used for numerical computing, which provides a powerful array object for handling multidimensional data. However, the process of adding a border to a NumPy array can be challenging for beginners.

In this article, we will discuss how to add a border around a NumPy array using different techniques and functions provided by NumPy. We will also provide examples to demonstrate each method's implementation.

How to add a border around a NumPy array?

There are multiple techniques available in NumPy that allow us to add a border around a NumPy array. Here are three commonly used approaches −

Method 1: Padding with zeroes

In this approach, we produce a larger array by attaching or adding rows and columns of zeroes around the original array. The magnitude of the boundary establishes the number of rows and columns to be attached. The numpy.pad() function provided by NumPy can be employed to carry out this process by designating the preferred boundary size and padding approach.

Method 2:Padding with constant values

Comparable to padding with zeroes, this approach involves adding a boundary of constant values rather than zeroes. The numpy.pad() function enables us to specify a constant value to be utilized for padding.

The method of adding a border around a NumPy array with constant values involves determining the dimensions of the new array with the border included. We then use the numpy.pad() function and specify the padding width and the mode as "constant" to fill the array with constant values. Additionally, we can choose the specific constant value to be used. The numpy.pad() function generates a new array with the specified dimensions and the padding area filled with the constant value.

Method 3:Concatenation

In this method, we produce a larger array by concatenating the original array with additional rows and columns on all sides. NumPy's numpy.concatenate() function is employed to combine the original array with the required number of rows and columns.

Initially, we generate distinct arrays filled with zeros, representing the top, bottom, left, and right borders. These arrays possess the desired dimensions. Subsequently, we merge these border arrays with the original array by utilizing the numpy.concatenate() function. Consequently, a new array is formed, encompassing the original array enclosed by the newly added border. This technique offers a convenient means to increase the array's dimensions while preserving its existing data.

These methods provide flexibility in adding borders of diverse magnitudes and values around a NumPy array, allowing for customization based on particular requirements.

Below is the program that shows how to add a border around a NumPy array using all three methods −

Example

import numpy as np

#function for adding border using Zero-padding method:
def add_border_zero_padding(arr, border_width):
   height, width = arr.shape
   new_height = height + 2 * border_width
   new_width = width + 2 * border_width

   new_arr = np.pad(arr, pad_width=border_width, mode='constant')

   return new_arr

#function for adding border using Constant-padding method:
def add_border_constant_padding(arr, border_width, constant_value):
   height, width = arr.shape
   new_height = height + 2 * border_width
   new_width = width + 2 * border_width

   new_arr = np.pad(arr, pad_width=border_width, mode='constant', constant_values=constant_value)

   return new_arr

#function for adding border using concatenation method:
def add_border_concatenation(arr, border_width):
   height, width = arr.shape
   new_height = height + 2 * border_width
   new_width = width + 2 * border_width

   top_border = np.zeros((border_width, width), dtype=arr.dtype)
   bottom_border = np.zeros((border_width, width), dtype=arr.dtype)
   left_border = np.zeros((new_height, border_width), dtype=arr.dtype)
   right_border = np.zeros((new_height, border_width), dtype=arr.dtype)

   new_arr = np.concatenate((top_border, arr, bottom_border), axis=0)
   new_arr = np.concatenate((left_border, new_arr, right_border), axis=1)

   return new_arr

# Example usage
original_array = np.array([[1, 2, 3], [4, 5, 6],  [7, 8, 9]])

border_width = 1
constant_value = 99

bordered_array_zero_padding = add_border_zero_padding(original_array, border_width)
bordered_array_constant_padding = add_border_constant_padding(original_array, border_width, constant_value)
bordered_array_concatenation = add_border_concatenation(original_array, border_width)
print("Original Array:")
print(original_array)
print("\nBorder using Zero Padding:")
print(bordered_array_zero_padding)
print("\nBorder using Constant Padding:")
print(bordered_array_constant_padding)
print("\nBorder using Concatenation:")
print(bordered_array_concatenation)

Output

C:\Users\Tutorialspoint>python image.py
Original Array:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Border using Zero Padding:
[[0 0 0 0 0]
 [0 1 2 3 0]
 [0 4 5 6 0]
 [0 7 8 9 0]
 [0 0 0 0 0]]

Border using Constant Padding:
[[99 99 99 99 99]
 [99  1  2  3 99]
 [99  4  5  6 99]
 [99  7  8  9 99]
 [99 99 99 99 99]]

Border using Concatenation:
[[0 0 0 0 0]
 [0 1 2 3 0]
 [0 4 5 6 0]
 [0 7 8 9 0]
 [0 0 0 0 0]]

Conclusion

In conclusion, adding a border around a NumPy array provides a useful way to expand its dimensions for various applications. Whether using zero-padding, constant-padding, or concatenation, NumPy offers versatile techniques to achieve this, allowing customization based on specific needs.

Updated on: 24-Jul-2023

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements