About Checking if a File is an Image

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

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