Blur A Video Using OpenCV In Python | Python Project

by | Jan 31, 2022 | Coding, Python

Home » DIY » Blur A Video Using OpenCV In Python | Python Project

Introduction of the Project

Today, we will learn to blur a video using OpenCV in Python. OpenCV is a library used to perform computer vision tasks. So, here is our python code that will help us to blur a video.

So, what are you waiting for? Let’s begin!



1. All you need is a Python 3.9 interpreter and IDLE (online or system configured)

NOTE: A certain file needs to be preinstalled for the code’s smooth running, due to which IDLE or IDE is a higher preferred choice.

2. OpenCV and Numpy modules must be preinstalled on your PC.

3. A demo video for it to process the given commands through code (here blurring a video).

Steps To Blur A Video Using OpenCV In Python

Step 1: Install OpenCV and NumPy if you don’t have them in your system.

Paste the below line of command in your command prompt and press enter.

pip install opencv-python

pip install numpy

Step 2: Paste the below code in your IDE/ Code Editor like VSCode

Source Code

# Import cv2 and numpy modules
import cv2
import numpy as np
# Here we are creating an object to capture our video
# Change the video path according to your video path and its name
capt = cv2.VideoCapture('videos/InputVideo.mp4')
# Message, to be displayed in case of an error while opening the video
if (capt.isOpened() == False):
print('An Error occurred while opening! Kindly check again.')
# To get the frame's width and height
frame_width = int(capt.get(3))
frame_height = int(capt.get(4))
# Here, we are defining object to write the video and its location
result = cv2.VideoWriter('videos/BlurredVideo.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 30, (frame_width, frame_height))
# Using a loop, we are reading the video, until end of the it

# We are capturing each frame of the video
ret, frame =
if ret == True:

# Here, we are performing our main task of blurring by adding gaussian blurring to the frame
frame = cv2.GaussianBlur(frame, (5, 5), 0)
# Here, we are saving the video frame
# Here, we are displaying the frame
cv2.imshow('Video', frame)
# To exit the screen using the keyboard, press 'e' to exit
if cv2.waitKey(27) & 0xFF == ord('e'):
# If no frame is found, end the loop
# Using, this, we are releasing the video
# Atlast, close all the GUI windows


This is the screenshot of the input video which we have used to blur for our demo.

Blur A Video Using OpenCV In Python | Python Project

And here is the screenshot of the video after it has been blurred using our code.

Blur A Video Using OpenCV In Python | Python Project

Explanation Of The Code

In the beginning, we imported cv2 and NumPy modules.

1. Firstly, we have created an object to capture our input video. And also, given a condition that if the video is not opening due to some error or the file is not a video file, then an error message will be displayed.

2. Now, we are getting the frame’s width and height. The blurred video needs to be stored somewhere as the final output, so we have defined an object and location where the output as a blurred video will be stored.

3. Then, using a loop, we are reading the video until the end of it, capturing each frame of the video & performing our main task of blurring by adding gaussian blurring to the frame, and saving the video frame and displaying it simultaneously.

4. To end the process, we have implemented one more functionality; that is, by pressing ’E’ we will be able to quit the process. At last, we are closing all the GUI windows.

Things to Remember

  • Python is case sensitive language; hence, don’t change the cases unnecessarily.
  • Import both the above-required modules using a command prompt to avoid errors before using the code.
  • Set the path of the video with its name before proceeding with the above-given code(Blur A Video Using OpenCV) in your system.

You May Also Like To Create…


Submit a Comment

Your email address will not be published. Required fields are marked *