Last Updated : 11 Aug, 2025
The cv2.copyMakeBorder() function in OpenCV allows us to add a border around an image. This can be useful for various image processing tasks such as image padding, creating frames or preparing images for machine learning. In this article, we'll see how to use this function with different types and examples.
Lets see a basic example:
Python
import cv2
from google.colab.patches import cv2_imshow
image = cv2.imread("/content/geeks14.png")
image = cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, None, value = 0)
print(window_name)
cv2_imshow(image)
Output:
Basic exampleThe output will be the original image with a 10-pixel black border added on all sides.
Syntax:
cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value)
Parameters:
Return Value: It returns an image.
Different Border TypesThe borderType parameter controls the style of the border we add to the image. Let's see some common options:
Here we will see how different border types affect the image. Each border type has a unique way of extending the edges of the image, either by adding solid colors, reflecting the edges or replicating the outermost pixels. This customization can be useful for a variety of image processing tasks.
Python
import cv2
from google.colab.patches import cv2_imshow
image = cv2.imread("/content/geeks14.png")
bordered_image_reflect = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT)
bordered_image_reflect_101 = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT_101)
bordered_image_replicate = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REPLICATE)
print("Border with Reflect")
cv2_imshow(bordered_image_reflect)
print("Border with Reflect_101")
cv2_imshow(bordered_image_reflect_101)
print("Border with Replicate")
cv2_imshow(bordered_image_replicate)
Output:
Border with Reflect Border with Reflect_101 Border with Replicate Example 2: Customizing Border Color with fill_valueWhen using a constant border, we can choose the color for the border by setting the value parameter. This flexibility is helpful when we need to apply a specific color such as for creating frames or preparing images for machine learning tasks.
Python
import cv2
from google.colab.patches import cv2_imshow
image = cv2.imread('/content/geeks14.png')
bordered_image = cv2.copyMakeBorder(image, 10, 10, 10, 10,
cv2.BORDER_CONSTANT, value=(0, 0, 255))
print('Bordered Image with Red Border')
cv2_imshow(bordered_image)
Output:
Bordered Image with Red Border Use Cases for cv2.copyMakeBorder()Adding borders to images can be helpful in various scenarios:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4