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

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build a Multimodal RAG System using AWS Bedrock and FAISS
In this LLM RAG Project, you will learn to build a Multimodal RAG system for a restaurant aggregator app, integrating text and visuals to deliver personalized food recommendations using advanced technologies like Amazon S3, Amazon Bedrock, and FAISS.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

OSZAR »