Nico Schlömer
Nico Schlömer

Reputation: 58841

image checksums for asserting unchanged visual output

I have a library that produces images from code, and I would like to write a capability test suite that loops over a number of test cases and does the following:

  1. Create a PNG image from the test case,
  2. create a checksum of the image, and
  3. assert that the checksum is the same as the reference checksum.

By comparing with precomputed reference checksums, I can be sure that the behavior or the library doesn't change without checking the images pixel by pixel.

I noticed that when creating PNGs, no two files are exactly alike although they look alike. I suppose a time stamp of sorts is always stored in the file. This rules (plain) PNG files out for this purpose.

What is an appropriate file format/hash algorithm for asserting that the visual output doesn't change?

Upvotes: 0

Views: 144

Answers (1)

Glenn Randers-Pehrson
Glenn Randers-Pehrson

Reputation: 12465

Rather than writing an application to do that, you could just use ImageMagick. Here's an example of converting a GIF to a PNG, to a PPM, and to a JPG and then checking to see if the pixel data is the same, no matter how it's encoded:

# convert logo.gif logo.png
# convert logo.png logo.ppm
# convert logo.png logo.jpg
# identify -verbose logo.gif logo.jpg logo.png logo.ppm | grep signature
signature: 5c701306a9a985a0c93c8d11a1e761d7f8637577697fc60d7189b221388f8edf
signature: 97fee507ef8464e2a9be00e65c615aa096e30abc925113ff5a43c4dbf0f94513
signature: 5c701306a9a985a0c93c8d11a1e761d7f8637577697fc60d7189b221388f8edf
signature: 5c701306a9a985a0c93c8d11a1e761d7f8637577697fc60d7189b221388f8edf

ImageMagick is open source so you can get a copy and see what it does. It promotes each image to 64-bit RGBA and then calculates an SHA hash over the resulting array of pixels. Notice that the three lossless formats all have the same signature, while logo.jpg has a different signature due to the lossy encoding.

Upvotes: 1

Related Questions