主要用于图片是否可以正常读取,校验图片内容,以及图片格式.

def verify(self):
    """
        Verifies the contents of a file. 
        For data read from a file, this
        method attempts to determine if the file is broken, without
        actually decoding the image data.  
        If this method finds any
        problems, it raises suitable exceptions.  
        If you need to load
        the image after using this method, you must reopen the image
        file.
        """
    pass


Image.verify()

使用示例:

from PIL import Image

try:
    image = Image.open('fire.jpg')  # 检查文件是否能正常打开
    image.verify()  # 检查文件完整性
    image.close()
except:
    try:
        image.close()
    except:
        pass
    raise
else:
    print('Image OK, format is %s.' % image.format)
Last modification:November 15th, 2022 at 02:04 pm