How do I programmatically check whether an image (PNG, JPEG, or GIF) is corrupted?

前端 未结 5 1731
你的背包
你的背包 2020-12-13 10:40

Okay. So I have about 250,000 high resolution images. What I want to do is go through all of them and find ones that are corrupted. If you know what 4scrape is, then you kno

相关标签:
5条回答
  • 2020-12-13 10:50

    An easy way would be to try loading and verifying the files with PIL (Python Imaging Library).

    from PIL import Image
    
    v_image = Image.open(file)
    v_image.verify()
    

    Catch the exceptions...

    From the documentation:

    im.verify()

    Attempts to determine if the file is broken, without actually decoding the image data. If this method finds any problems, it raises suitable exceptions. This method only works on a newly opened image; if the image has already been loaded, the result is undefined. Also, if you need to load the image after using this method, you must reopen the image file.

    0 讨论(0)
  • 2020-12-13 10:53

    You could use imagemagick if it is available:

    if you want to do a whole folder

    identify "./myfolder/*" >log.txt 2>&1
    

    if you want to just check a file:

    identify myfile.jpg
    
    0 讨论(0)
  • 2020-12-13 11:04

    i suggest you check out imagemagick for this: http://www.imagemagick.org/

    there you have a tool called identify which you can either use in combination with a script/stdout or you can use the programming interface provided

    0 讨论(0)
  • 2020-12-13 11:06

    If your exact requirements are that it show correctly in FireFox you may have a difficult time - the only way to be sure would be to link to the exact same image loading source code as FireFox.

    Basic image corruption (file is incomplete) can be detected simply by trying to open the file using any number of image libraries.

    However many images can fail to display simply because they stretch a part of the file format that the particular viewer you are using can't handle (GIF in particular has a lot of these edge cases, but you can find JPEG and the rare PNG file that can only be displayed in specific viewers). There are also some ugly JPEG edge cases where the file appears to be uncorrupted in viewer X, but in reality the file has been cut short and is only displaying correctly because very little information has been lost (FireFox can show some cut off JPEGs correctly [you get a grey bottom], but others result in FireFox seeming the load them half way and then display the error message instead of the partial image)

    0 讨论(0)
  • 2020-12-13 11:11

    In PHP, with exif_imagetype():

    if (exif_imagetype($filename) === false)
    {
        unlink($filename); // image is corrupted
    }
    

    EDIT: Or you can try to fully load the image with ImageCreateFromString():

    if (ImageCreateFromString(file_get_contents($filename)) === false)
    {
        unlink($filename); // image is corrupted
    }
    

    An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognized format, or the image is corrupt and cannot be loaded.

    0 讨论(0)
提交回复
热议问题