Check an Image is Corrupted or Broken Using Python – Python Tutorial

By | January 8, 2021

When we are using python to download images from internet, we should check these images are downloaded completely or not. In this tutorial, we will introduce you how to do.

Check an Image is Corrupted or not Using Python - Python Tutorial

Check an Image is Corrected or not Using Python

If an image is not downloaded completely, it is corrupted. We can use python to check it corrupted or not.

In this tutorial, we will use python pillow to implement it.

Preliminary

We should install and import python pillow first.

from PIL import Image

Then we will create a function to check image.

Create a python function to check image corrupted or not

Here is an example code:

def isCorrupted(fileimage):
    try:
        with Image.open(fileimage) as img:
            img.verify() # verify that it is, in fact an image
        return False
    except Exception as e:
        print(e)
        return True

In this example, we will use Image.verify() to check an image corrupted or not. If it is corrupted, this function will return True.

How to use this function?

We can use this function as follow:

filename = 'paging.jpg'
print(isCorrupted(filename))

Leave a Reply

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