tags:

views:

279

answers:

3

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?

+1  A: 

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).
mlathe
Will imagemagick still fail if the png has a valid header?
Rook
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).
mlathe
+3  A: 

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.)

Brooks Moses
A: 

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)

ChristopheD