About Checking if a File is an Image

You can check if a file is an image with this function:

1
2
3
4
5
6
7
def is_image(file_path):
    from PIL import Image
    try:
        Image.open(file_path).verify()
    except IOError:
        return False
    return True

Tips and Tricks Programming Python 3