How to Check Image Blur or Not using Python?

  10311 views   2 years ago Python

In this article, I will share with you how to check image blur or not using the python script. some days ago i was working on one application and in that application i need built functionality bank cheque OCR. user upload the cheque image and system detect some needed text data from image.

Why i need implement?

In OCR functionality user upload image in application. but some time user uploaded image not with the good quality. so, i add check image blur or not functionality on uploaded image and this can be help me if uploaded image was blur then system rejected it and send some warning to user.

Step - 1: Install pip

A series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.

pip install imutils

NumPy is the fundamental package for array computing with Python.

pip3 install numpy

Wrapper package for OpenCV python bindings.

pip install opencv-python

Step - 2: Write Python Script

from imutils import paths
import argparse
import cv2
import sys
def variance_of_laplacian(image):
    return cv2.Laplacian(image, cv2.CV_64F).var()
#path of image url
imagePath =sys.argv[1]	
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = variance_of_laplacian(gray)
text = "Not Blurry"
if fm < 100:
    text = "Blurry"	
print text

i hope this will be help a lot.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]