Take the 2-minute tour ×
Software Recommendations Stack Exchange is a question and answer site for people seeking specific software recommendations. It's 100% free, no registration required.

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
share|improve this question
1  
Just to clarify, the metadata (e.g. EXIF) is not relevant, right? –  Cristian Ciupitu Jul 13 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 at 9:01

5 Answers 5

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.

share|improve this answer

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
c=a(:);
d=b(:);

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

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?

share|improve this answer
1  
Wouldn't something like > 0.95 be better than == 1 to compensate for compression artifacts in case of lossy compression? –  vsz Jul 14 at 3:12
    
@vsz I haven't tried it but that sounds reasonable. –  Franck Dernoncourt Jul 14 at 3:29
    
@vsz Actually asker wants exactly the same graphically, so == 1 is correct. –  Nicolas Raoul Jul 17 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.

share|improve this answer
3  
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 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/.

share|improve this answer

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:

#!/bin/bash

# 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!"
else
    echo "The images are different! Bleh.."
fi
share|improve this answer
1  
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 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 at 14:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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