Thursday, October 2, 2025
HomeLanguagesCheck if the image is empty using Python – OpenCV

Check if the image is empty using Python – OpenCV

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!!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7079 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS