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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

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.