How to create borders around the images using OpenCV

This recipe helps you create borders around the images using OpenCV

Recipe Objective: How to create borders around the images using OpenCV?

In many cases, we would like to add borders to our images. In this recipe, let us understand how to create borders (Also known as padding) around the images using OpenCV.

Step 1: Importing libraries and reading the image

Let us first import the cv2 library and read the image using the cv2.imread() function. The image that we are using here is the one given below.

ProjectPro Logo
import cv2
image = cv2.imread('project.jpg')

Step 2: Creating border using cv2.copyMakeBorder() function

After reading the image, we can create a border using the cv2.copyMakeBorder() function. This function takes the following input parameters

  • src: The image on which we have to draw a border
  • top: Width of the border in the top.
  • bottom: Width of the border in the bottom
  • left: Width of the border in the left
  • right: Width of the border in the right
  • borderType: Type of the border

Let us try to create a simple black equi-sized border of width 15 around the image. For this purpose, we pass cv2.BORDER_CONSTANT to the borderType parameter. We shall discuss various kinds of boundaries exclusively in the following recipe.

image_bordered = cv2.copyMakeBorder(src=image, top=15, bottom=15, left=15, right=15, borderType=cv2.BORDER_CONSTANT)

Step 3: Displaying the Image

Now we are done with adding the border to the image. Let us see how the image looks by displaying the image using the cv2.imshow() function.

cv2.imshow('Bordered Image',image_bordered)
cv2.waitKey(0)

Output:

Bordered image

And yes, we can see a black border around the image!

Download Materials

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.