An efficient way to detect corrupted png files?

12,587

Solution 1

A different possible solution would be to slightly change how your processor processes the files: Have it always create a file named temp.png (for example), and then rename it to the "correct" name once it's done. That way, you know if there is a file named temp.png around, then the process got interrupted, whereas if there is no such file, then everything is good.

(A variant naming scheme would be to do what Firefox's downloader does -- append .partial to the real filename to get the temporary name.)

Solution 2

I know this is a question from 2010, but I think this is a better solution: pngcheck.

Solution 3

Since you're on a Linux system you probably already have Python installed.

An easy way would be to try loading and verifying the files with PIL (Python Imaging Library) (you'd need to install that first).

from PIL import Image

v_image = Image.open(file)
v_image.verify()

(taken verbatim from my own answer in this thread)

Solution 4

Kind of a hack, but works If you are running on linux or something like you might have the "convert" command

$ convert --help
Version: ImageMagick 5.5.6 04/01/03 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 2003 ImageMagick Studio LLC

Usage: convert [options ...] file [ [options ...] file ...] [options ...] file

If you make an invalid png, and then try to convert, you'll get an error:

$ date> foo.png
$ convert foo.png foo.gif
convert: NotAPNGImageFile (foo.png).
Share:
12,587
dave
Author by

dave

Updated on June 28, 2022

Comments

  • dave
    dave almost 2 years

    I've written a program to process a bunch of png files that are generated by a seperate process. The capture mostly works, however there are times when the process dies and is restarting which leaves a corrupted image. I have no way to detect when the process dies or which file it dies one (there are ~3000 png files).

    Is there a good way to check for a corrupted png file?

  • mlathe
    mlathe about 14 years
    yea, looks like you can corrupt a png in some ways that cause the convert to not complain. (like doing an "echo corrupt >> valid.png" and doing a convert).