virtualweb has asked for the
wisdom of the Perl Monks concerning the following question:

Hi:

I would like my script to compare two images and tell me if even one single pixel is slightly different in one of them. Also tell me if its width or height is different too.

Is there a library which already does this..??

I know about the file test operators. (-T for text files, -B for Binary, -s for size, -M last modified in days, -A last accessed in days), but I havent found any operator which can tell me if the pixels or width or height have been changed.

So far, besides using the test operators above, I'm opening both binary files , reading them into memory and comparing the output... but ofcourse this is not good enough I need better suggestions.

Really? IMHO png and jpg use compression methods resulting in unpredictable information loss. IMHO converting them to a raw format will not often produce identical data.

Really. And to resolve your doubts please try it.

The "secret" such as it is, is that the raw format used for the comparison is neither jpg nor png, but libgd's own internal format, which is an uncompressed, lossless format. The images of all formats supported by GD are convertedfully expanded into this internal format, and it is this binary representation, returned by the $image->gd method that is being compared in the code I posted. As such, even individual pixel differences are detected.

By way of demonstration, in my post above, I took a jpg image of a mandrill monkey and used an image editor to convert it to a compressed but lossless .png. I then compared those two images and no discrepancies were found.

By way of further demonstration, using the slightly expanded version below, I then edited the ,png version by changing the color of a single pixel on the mandrill's nose, from it's original near white color to pure red. and then compared that against the original .jpg:

As you can see, that single pixel difference was correctly detected by the raw binary compare, and then subsequently isolated using a pixel-by-pixel comparison. Please try it to convince yourself that it is correct.

But do bear in mind my warning above. If, for example, you were to take a ,png image and convert it to a lossy image format such as .jpg, then although the images may appear superficially similar to the human eye, they may be quite different in actuality. This due to the jpeg algorithms practice of substituting a single color for many, closely aligned but mathematically different colors in order to reduce palette sizes and aid compression.

Such lossy transforms will be correctly detected as different, even though cursory, by-eye inspections might adjudge them to be the same.

The Image Magick library does it all, and it has a Perl interface.... see perl magick usage
Here is a simple usage of IM, to list all pixels,.... just in case you want to test just one certain pixel. :-)

Ada Lovelace for the palindrome
Albert Einstein for having smelly feet
Alfred Nobel for his contribution to battlefield science
Burkhard Heim for providing the missing link between science and mysticism
Claude Shannnon for riding a unicycle at night at MIT
Donald Knuth for being such a great organist
Edward Teller for being the template for Dr. Strangelove
Edwin Hubble for pretending to be a pipe-smoking English gentleman
Erwin Schrödinger for cruelty to cats
Hedy Lamarr for weaponizing pianos
Hugh Everett for immortality, especially for cats
Isaac Newton for his occult studies
Kikunae Ikeda for discovering the secrets of soy sauce
Larry Wall for his website
Louis Camille Maillard for discovering why steaks taste good
Marie Curie for the shiny stuff
Nikola Tesla for the cool cars
Paul Dirac for speaking one word per hour when socializing
Richard Feynman for his bongo skills
Robert Oppenheimer for his in-depth knowledge of the Bhagavad Gita
Rusi P Taleyarkhan for Cold Fusion
Sigmund Freud for his Ménage ā trois
Theodor W Adorno for his contribution to the reception of jazz
Wilhelm Röntgen for the foundations of body scanners
Yulii Borisovich Khariton for the Tsar Bomba
Other (please explain why)