An efficient way to detect corrupted png files?
Asked Answered
P

5

6

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?

Possessive answered 5/3, 2010 at 1:31 Comment(1)
" I have no way to detect when the process dies or which file it dies on" - Why?Pelt
B
4

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

Broadfaced answered 5/3, 2010 at 1:46 Comment(0)
S
8

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)

Swiftlet answered 5/3, 2010 at 9:17 Comment(0)
E
8

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

Erde answered 5/4, 2015 at 20:57 Comment(0)
B
4

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

Broadfaced answered 5/3, 2010 at 1:46 Comment(0)
C
2

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).
Cece answered 5/3, 2010 at 1:39 Comment(1)
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).Cece
M
0

Find all non-PNG files:

find . -type f -print0 | xargs -0 file --mime | grep -vF image/png

Find all corrupted PNG files:

find . -type f -print0 | xargs -0 -P0 sh -c 'magick identify +ping "$@" > /dev/null' sh
  1. file command only checks magic number. Having the PNG magic number doesn't mean it is a well formed PNG file.
  2. magick identify is a tool from ImageMagick. By default, it only checks headers of the file for better performance. Here we use +ping to disable the feature and make identify read the whole file.
Mariellamarielle answered 3/3, 2022 at 6:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.