The cv2.copyMakeBorder() function in OpenCV allows us to add a border around an image. Common use cases include:
- Padding images to make them a fixed size for neural network input
- Adding visual frames or margins around images for better display
- Extending image boundaries to avoid edge loss during filtering or convolution
Example: This example demonstrates how to add a uniform black border around an image using cv2.copyMakeBorder() function and display the bordered image in a window using OpenCV.
Note: For this article sample image is used "logo.png", to download click here.
import cv2
image = cv2.imread("logo.png")
image = cv2.copyMakeBorder( image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(0, 0, 0))
cv2.imshow("Bordered Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

Explanation:
- cv2.imread("logo.png") loads the image from the local directory.
- cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT) adds a constant border of 10 pixels on all sides of the image.
- value=(0, 0, 0) sets the border color to black (BGR format).
- cv2.imshow("Bordered Image", image) displays the bordered image in a window.
- cv2.waitKey(0) waits for a key press, and cv2.destroyAllWindows() closes all OpenCV windows.
Syntax
cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value=None)
Parameters:
- src: Source image to which the border is added.
- top: Border width at the top of the image (in pixels).
- bottom: Border width at the bottom of the image (in pixels).
- left: Border width on the left side of the image (in pixels).
- right: Border width on the right side of the image (in pixels).
- borderType: Type of border to be added (for example, cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT).
- value: Color of the border. This parameter is used only with cv2.BORDER_CONSTANT.
Different Border Types
The borderType parameter controls the style of the border added to the image. Some common options are:
- cv2.BORDER_CONSTANT: Adds a border with a constant color. The color is specified using the value parameter for example, value=(0, 0, 255) for a red border.
- cv2.BORDER_REFLECT: Border is a mirror reflection of the edge pixels. For example, if the image contains the sequence "abcdef", the border would be reflected as "gfedcba|abcdef|gfedcba".
- cv2.BORDER_REFLECT_101 (or cv2.BORDER_DEFAULT): Similar to BORDER_REFLECT but with a slight difference. If the image is "abcdefgh", the output will be "gfedcb|abcdefgh|gfedcba".
- cv2.BORDER_REPLICATE: Border is filled by replicating the outermost pixels of the image. For example, if the image is "abcdefgh", the output will be "aaaaa|abcdefgh|hhhhh".Â
Examples
Example 1: Here, different border types is applied to the same image to observe how each type extends the image edges.
import cv2
image = cv2.imread("logo.png")
border_reflect = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT)
border_reflect_101 = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT_101)
border_replicate = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REPLICATE)
cv2.imshow("Border Reflect", border_reflect)
cv2.imshow("Border Reflect 101", border_reflect_101)
cv2.imshow("Border Replicate", border_replicate)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output



Explanation:
- cv2.copyMakeBorder(..., cv2.BORDER_REFLECT) creates a border by reflecting edge pixels.
- cv2.copyMakeBorder(..., cv2.BORDER_REFLECT_101) reflects pixels without repeating the edge pixel.
- cv2.copyMakeBorder(..., cv2.BORDER_REPLICATE) fills the border using the outermost pixel values.
Example 2: In this example, we add a constant-colored border around an image by setting the value parameter in cv2.copyMakeBorder().
import cv2
image = cv2.imread("logo.png")
bordered_image = cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(0, 0, 255))
cv2.imshow("Red Border Image", bordered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

Explanation:
- cv2.copyMakeBorder(..., cv2.BORDER_CONSTANT) adds a constant border.
- value=(0, 0, 255) specifies the border color in BGR format, resulting in a red border.
Limitations
While cv2.copyMakeBorder() is useful for adding padding around images, it has a few constraints that should be considered before using it in real-world applications.
- Fixed Border Size: Border size must be specified manually.
- Limited Border Types: Only predefined border styles are available.
- Color Restriction with BORDER_CONSTANT: Suitable for solid colors but not complex patterns.
- No Automatic Resizing: The function does not adjust image aspect ratio automatically.