I need a command line tool that compares 2 images and says if their contents are the same, regardless of encoding - i.e. one might be a *.bmp and the other might be a *.png, so long as all their width, height and all the corresponding pixels are the same.

  • Exact graphical sameness is needed
  • Compression loss, even if nearly invisible, makes a different image
  • Same alpha-transparency is also important
  • EXIF/etc irrelevant
  • 1
    Just to clarify, the metadata (e.g. EXIF) is not relevant, right? – Cristian Ciupitu Jul 13 '14 at 18:40
  • 1
    @CristianCiupitu Yes, just that the images display the same on any background (i.e. alpha-transparency is a difference). – sashoalm Jul 14 '14 at 9:01

With ImageMagick (apt-get install imagemagick), you can compare images independent of encoding and metadata like this:

identify -quiet -format "%#" images...

Note that images that have been encoded with lossy compression like JPEG (*.jpg) have subtle, often invisible changes.

See also ImageMagick Examples: Image Signatures.

Strictly speaking, you need to compare the color model, and scaling of pixel values, too. They may not be part of the metadata in the image.

  • 2
    Great answer. Just to clarify though: this will print concatenated checksums for each image in images. Then you need to check whether these checksums are the same. – Clément Jul 10 '16 at 14:52

If you have MATLAB, you can use:

% Reading images as array to variable 'a' & 'b'. 
a = imread('MIMICDatacollection.bmp'); 
b = imread('MIMICDatacollection.png'); 

% Flatten multidimensional arrays to 1D

% Perform comparison
if length(c) ~= length(d)
    disp('The images do not have the same size') 
    e = corr2(c,d);           
    if e==1 
        disp('The images are same')
        disp('The images are not same') 

Personally, I use it with PNG and BMP, but it should work for any format supported by imread.

If you need to run it on a machine that doesn't have Matlab, you can turn it into a function and compile it to make it CLI.

If you don't have Matlab that should be easy to port in any high-level language with a decent imaging library, such as Python Imaging Library (PIL).

Related: How can I tell if I am downloading/saving duplicate images?

  • 2
    Wouldn't something like > 0.95 be better than == 1 to compensate for compression artifacts in case of lossy compression? – vsz Jul 14 '14 at 3:12
  • @vsz I haven't tried it but that sounds reasonable. – Franck Dernoncourt Jul 14 '14 at 3:29
  • @vsz Actually asker wants exactly the same graphically, so == 1 is correct. – Nicolas Raoul Jul 17 '14 at 5:43

findimagedupes - Finds visually similar or duplicate images

findimagedupes is a commandline utility which performs a rough "visual diff" to two images. This allows you to compare two images or a whole tree of images and determine if any are similar or identical. On common image types, findimagedupes seems to be around 98% accurate.

  • 4
    Unfortunately doesn't work for my case as it reports visually similar (but not same) images as duplicates. I need it verify results of automated tests, so exact sameness is needed, not just similarity. – sashoalm Jul 13 '14 at 14:29

I eventually created a small Qt program that I called imgdiff, which takes 2 filenames and performs a pixel-by-pixel comparison. It will print out an error message if they differ and exit with 1, or silently exit with 0 if they are the same.

Example usage would be:

imgdiff img1.png img2.bmp

Link to the Google Code project - https://code.google.com/p/imgdiff/.


If you want to see if the images are identical a slightly fun/different method springs to mind: get the base64 of an image (or indeed any file) and compare the two. You could put it in a Bash script like this:


# Get base64 encoded image
IMG1="$(cat ~/an_image.jpg | base64)"
IMG2="$(cat ~/another_image.jpg | base64)"

#See if the images are the same
if [ "$IMG1" == "$IMG2" ]; then
    echo "The images are the same! Yaaaaaaaaaay!"
    echo "The images are different! Bleh.."
  • 4
    Why use this script at all then? If I just wanted to compare the files I'd have used diff. I want to compare the images, though, so if you transcode a bmp to png, it will still say that it's the same image. – sashoalm Jul 15 '14 at 14:07
  • Steganography sort of came into my mind when I saw this question, little micro changes perhaps. This script will obviously only let a user know if the files are identical or not, nothing fancy (really I was just throwing a little fun piece into this).. – Elliot Reed Jul 15 '14 at 14:57
  • Are you aware of cmp? – Clément Jul 10 '16 at 14:15

Let’s say you have a folder named before which contains original images, and a folder after which should contain visually identical images with the same file name. Using ImageMagick’s compare, you can do this:

for file in before/*.png; do
    result=$(compare -metric AE "${file}" "${file/before/after}" /tmp/diff.png 2>&1);
    if [ "${result}" != '0' ]; then
        echo "${result} incorrect pixels in ${file}";

Might need tweaking to handle alpha channel appropriately but converting to ppm and checksumming seems to work:


find "${@:-.}" -type f -print |\
while IFS= read -r file; do
    hash=$(convert 2>&- "${file}" -strip ppm:- | md5sum) 
    # we get this hash if convert fails and produces no output
    [ "$hash" = 'd41d8cd98f00b204e9800998ecf8427e  -' ] \
    || echo "$hash ${file}"
done |\
sort | uniq -w32 --all-repeated=separate | sed 's/^.\{36\}//'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.