Open In App

Check if the image is empty using Python – OpenCV

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

Prerequisite: Basics of OpenCV

OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform that it is available in multiple programming languages such as Python, C++, etc. In this article, we’ll try to check if the opened image is empty or not by using OpenCV (Open Source Computer Vision)

To do this OpenCV libraries are required to install:

pip install opencv-python

To achieve this objective we will use cv2.imread() method, If this method read the image then it returns the image coordinates matrix otherwise it will return None.

Input Image:

Gfg.png

Example:

In this example, we will read the image and check found or not.

Python3




# Importing OpenCV library
import cv2
  
# user define function
# that return None or 
def check_empty_img(img):
    # Reading Image
    # You can give path to the 
    # image as first argument
    image = cv2.imread(img)
  
    # Checking if the image is empty or not
    if image is None:
        result = "Image is empty!!"
    else:
        result = "Image is not empty!!"
  
    return result
      
# driver node
img = "Gfg.png"
  
# Calling and printing
# the function
print(check_empty_img(img))


Output:

Image is not empty!!

Example 2:

In this example, here the image is not found.

Python3




# Importing OpenCV library
import cv2
  
# user define function
# that return None or 
def check_empty_img(url):
    # Reading Image
    # You can give path to the 
    # image as first argument
    image = cv2.imread(url)
  
    # Checking if the image is empty or not
    if image is None:
        result = "Image is empty!!"
    else:
        result = "Image is not empty!!"
  
    return result
      
# driver node
img = "geek.png"
  
# Calling and printing
# the function
print(check_empty_img(img))


Output:

Image is empty!!


Similar Reads

Convert OpenCV image to PIL image in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. 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
3 min read
OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
Content has been removed on Author's request.
1 min read
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
6 min read
Image resizing using Seam carving using OpenCV in Python
Seam carving is an effective image processing technique with the help of which an image can be resized without removing important elements from the image. The basic approach is to find all the continuous pixels with low energy from left to right or from top to bottom. After the region is selected, it is removed from the original image, leaving only
2 min read
Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
5 min read
Check if image contour is convex or not in OpenCV Python
In this article we will see different ways to check if an image contour is convex or not with help of python OpenCV. Contour: Contours are continuous curves that represent the boundaries of an object in an image. In the field of computer vision and image processing, contours are used to detect and analyze the shape and structure of objects in image
4 min read
Reading an image in OpenCV using Python
Prerequisite: Basics of OpenCV In this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library. Following types of files are supported in OpenCV library: Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun rasters - *.sr, *.ras TIFF files - *.tiff, *.tif Raste
5 min read
Find the Solidity and Equivalent Diameter of an Image Object Using OpenCV Python
In this article, we will see how we can find the solidity and the equivalent diameter of an object present in an image with help of Python OpenCV. Function to Find Solidity The solidity of an image is the measurement of the overall concavity of a particle. We can define the solidity of an object as the ratio of the contour area to its convex hull a
4 min read
Python | Detect corner of an image using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. Let's see how to detect the corner in the image. cv2.goodFeaturesToTrack() method finds N strongest corners in the image by Shi-Tomasi metho
2 min read