2

I would like to recursively compare screenshot files in a directory. I tried using cmp but it always returns a difference - even if the images are not visually different - I guess the difference in the file must be the last changed and last modified dates.

Is there a way I could only compare the pixel content of the image files while ignoring these headers?

pax
  • 482
  • 8
  • 26
  • This might be a tough one. Even if images "look" the same, their files may vary in many ways - perhaps a different encoding algorithm is used, or the images have different meta information. OSX saves its screenshots in .png format which is supposed to be lossless http://en.wikipedia.org/wiki/Portable_Network_Graphics, but I still wouldn't count on two different implementations to produce identical encoding. Probably your best bet would be to convert both images to some simple format (e.g. tiff or bmp) and compare those. The `sips` tool in OSX can do that. `man sips` – Digital Trauma Oct 26 '13 at 22:57
  • I my particular case it's slightly easier, I need to compare screenshots taken in the same environment, I just want to eliminate duplicate screenshots taken with `screencapture` – pax Oct 28 '13 at 16:40

2 Answers2

6

How about using compare from the ImageMagic software-suite (http://www.imagemagick.org). Available for mac and all modern Linux distributions.

I'm not that familiar with comparing images, but I tried creating some samples and ran following snippets,

$ compare -identify -metric MAE same1.png same2.png null
>> same1.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 1.64KB 0.010u 0:00.009
>> same2.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 1.64KB 0.010u 0:00.000
>> 0 (0)

$ compare -identify -metric MAE same1.png diff.png null
>> same1.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 1.64KB 0.010u 0:00.020
>> diff.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 6.01KB 0.000u 0:00.009
>> 209.225 (0.00319257)

And it seems to work as expected.

Hope that helps!

Edit, good point by DigitalTrauma, comparing between different formats/compression algorithms may be a problem,

$ compare -identify -metric MAE same1.png same2.xcf null
>> same1.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 1.64KB 0.080u 0:00.040
>> same2.xcf[0] XCF 640x400 640x400+0+0 8-bit DirectClass 2.73KB 0.070u 0:00.030
>> 0 (0)

$ compare -identify -metric MAE same1.png same2.bmp null
>> same1.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 1.64KB 0.010u 0:00.010
>> same2.bmp[0] BMP 640x400 640x400+0+0 8-bit DirectClass 768KB 0.000u 0:00.000
>> 0 (0)

$ compare -identify -metric MAE same1.png same2.jpg null
>> same1.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 1.64KB 0.010u 0:00.019
>> same2.jpg[0] JPEG 640x400 640x400+0+0 8-bit DirectClass 3.65KB 0.000u 0:00.009
>> 0.196766 (3.00245e-06)

So, when comparing against jpeg we get a difference, even though the pictures "look" the same. This is definitly not my area, but I don't think converting the pictures to the same format would make any difference since the compression (or whatever makes the pictures different) already is applied to the image.

$ convert same2.jpg same2-from-jpg.png
$ compare -identify -metric MAE same2.png same2-from-jpg.png null
>> same2.png[0] PNG 640x400 640x400+0+0 8-bit PseudoClass 256c 1.38KB 0.040u 0:00.020
>> same2-from-jpg.png[0] PNG 640x400 640x400+0+0 8-bit DirectClass 1.64KB 0.010u 0:00.000
>> 0.196766 (3.00245e-06)

Above we convert the jpg back to png and then compare it with the original, and it still differs.

Anyway, maybe this will give you some insight. I can definitely recommend ImageMagick when working with pictures.

2

I've found the following pieces of OSX software that can identify duplicate images by image content:

pdiff will definitely work from a bash script. The rest are more GUI oriented but may also have a command line interface.

Winter Dragoness
  • 1,327
  • 12
  • 13