The Wayback Machine - https://web.archive.org/web/20240624122813/https://www.geeksforgeeks.org/opencv-python-program-to-blur-an-image/
Open In App

OpenCV Python Program to blur an image

Last Updated : 04 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Note: This post contains codes that cannot be run using an online compiler. Please make sure that you have Python 2.7 and cv2 module installed before trying to run the program on your system.

Hi everyone! I read a brilliant work by Aditya Prakash – OpenCV C++ Program to blur an image, so I decided to come up with something similar but this time in Python. So, here is a very simple program with basically the same result.




# Python Program to blur image
  
# Importing cv2 module
import cv2 
  
# bat.jpg is the batman image.
img = cv2.imread('bat.jpg') 
  
# make sure that you have saved it in the same folder
# You can change the kernel size as you want
blurImg = cv2.blur(img,(10,10)) 
cv2.imshow('blurred image',blurImg)
  
cv2.waitKey(0)
cv2.destroyAllWindows()


Output:

Now, this program above is using image blurring technique called Averaging.There are some other options available as well – Gaussian Blurring, Median Blurring, Bilateral Filtering. Let’s make a couple of additions in our program and compare the results.




# importing opencv CV2 module
import cv2 
  
# bat.jpg is the batman image.
img = cv2.imread('gfg.png')
   
# make sure that you have saved it in the same folder
# Averaging
# You can change the kernel size as you want
avging = cv2.blur(img,(10,10))
   
cv2.imshow('Averaging',avging)
cv2.waitKey(0)
  
# Gaussian Blurring
# Again, you can change the kernel size
gausBlur = cv2.GaussianBlur(img, (5,5),0) 
cv2.imshow('Gaussian Blurring', gausBlur)
cv2.waitKey(0)
  
# Median blurring
medBlur = cv2.medianBlur(img,5)
cv2.imshow('Media Blurring', medBlur)
cv2.waitKey(0)
  
# Bilateral Filtering
bilFilter = cv2.bilateralFilter(img,9,75,75)
cv2.imshow('Bilateral Filtering', bilFilter)
cv2.waitKey(0)
cv2.destroyAllWindows()


Original Image:

Averaging:

Gaussian Blurring:

Media Blurring:

Bilateral Filtering:

Hope you enjoyed the post! Auf Wiedersehen!

About the author:

Vishwesh Shrimali is an Undergraduate Mechanical Engineering student at BITS Pilani. He fulfils about all the requirements not taught in his branch- white hat hacker, network security operator, and an ex – Competitive Programmer. As a firm believer in power of Python, his majority work has been in the same language. Whenever he get some time apart from programming, attending classes, watching CSI Cyber, he go for a long walk and play guitar in silence. His motto of life is – “Enjoy your life, ‘cause it’s worth enjoying!”

If you also wish to showcase your blog here, please see GBlog for guest blog writing on GeeksforGeeks.



Similar Reads

OpenCV C++ Program to blur an image
The following is the explanation to the C++ code to blur an Image in C++ using the tool OpenCV. Things to know: (1) The code will only compile in Linux environment. (2) Compile command: g++ -w article.cpp -o article `pkg-config --libs opencv` (3) Run command: ./article (4) The image bat.jpg has to be in the same directory as the code. Before you ru
5 min read
PyQt5 QLabel - Accessing blur radius of the blur effect
In this article we will see how we can access the blur radius of the label blur effect. The blur radius (required), if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be. By default the blur radius value is 5.0 we can change it any time.In order to do this we use blurRadius method. Syntax : blur_effect.blurRadius(
2 min read
PyQt5 QLabel - Setting blur radius to the blur effect
In this article we will see how we can set blur radius to the label blur effect, The blur radius (required), if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be. By default the blur radius value is 5.0 we can change it any time. In order to do this we use setBlurRadius method. Syntax : blur_effect.setBlurRadius(
2 min read
Java Program to Blur Images using OpenCV
Blurring is a simple and frequently used image processing operation. It is also called as Smoothing. OpenCV library provides many functions to apply diverse linear filters to smooth images or blur images. Smoothing of an image removes noisy pixels from the image and applying a low-pass filter to an image. A low-pass filter means removing noise from
4 min read
OpenCV C++ Program to blur a Video
The following is the explanation to the C++ code to blur a video in C++ using the tool OpenCV. Things to know: (1) The code will only compile in Linux environment. (2) To run in windows, please use the file: 'blur_video.o' and run it in cmd. However if it does not run(problem in system architecture) then compile it in windows by making suitable and
3 min read
OpenCV | Motion Blur in Python
This article explains how to add blur to an image using OpenCV. Motion blur is a specific type of blur used to lend a directed blur effect to images. The Motion Blur Filter Applying motion blur to an image boils down to convolving a filter across the image. Sample 5*5 filter filters are given below. Vertical: Horizontal: The greater the size of the
2 min read
Python OpenCV | cv2.blur() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.blur() method is used to blur an image using the normalized box filter. The function smooths an image using the kernel which is represented as: Syntax: cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) Parameters: src: It is the image whose is to be blur
2 min read
How to Blur Faces in Images using OpenCV in Python?
Prerequisite: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the num
2 min read
Blur and anonymize faces with OpenCV and Python
In this article, we are going to see how to Blur and anonymize faces with OpenCV and Python. For this, we will be using Cascade Classifier to detect the faces. Make sure to download the same, from this link: haarcascade_frontalface_default.xml ApproachFirstly, we use a built face detection algorithm, to detect the face from a real-time video or fro
4 min read
Faces Blur in Videos using OpenCV in Python
OpenCV is an open-source cross-platform library for various operating systems, including Windows, Linux, and macOS, for computer vision, machine learning, and image processing. With the help of OpenCV, we can easily process images and videos to recognize objects, faces, or even someone's handwriting. In this article, we will see how to blur faces i
3 min read
Practice Tags :
three90RightbarBannerImg