images = identical ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

images = identical ?

Post by boarders' paradise »

Hi. Sorry, a beginner question. Maybe you can help me ?

Using ImageMagick, I want to compare two images (with different file formats) and see if they are 100% identical (all pixels are the same).

I use :

Code: Select all

 compare.exe -metric AE %1 %2 differences.bmp 
... where %1 and %2 will be replaced by some file names.

-metric AE gives me the absolute amount of different pixels. This works for me. I get "0" if the files are identical and for example "202" if they are slightly different, etc.

Is this good, or is there a better ImageMagick command for this, or other parameters, etc. ?


In the end, I want to write a little batch file, using ImageMagick's compare.exe and write an output sentence, i.e. write in green "images are exactly the same!" if the output of the above equals zero or in red "images are different!" if the output of the above is any number greater than zero.

I just can't get it to work. I cannot "capture" ImageMagick's output and use it.

I tried to redirect the output into a text file like this:

Code: Select all

compare -metric AE %1 %2 thisfilewillbedeleted.bmp >thisfilewillbedeleted.txt
... but it doesn't work. I see "202" on my screen but the text file stays empty.

It's not a problem of the batch command. because if I use the verbose switch like this:

Code: Select all

compare -verbose -metric AE %1 %2 thisfilewillbedeleted.bmp >thisfilewillbedeleted.txt
... my text file is full of ImageMagick's output.

I also used FOR /F to achieve this instead of the redirection into a text file:

Code: Select all

 FOR /F "usebackq" %%i in (`compare.exe -metric AE %1 %2 thisfilewillbedeleted.bmp`) DO cecho {red} %%i
... but the result is the same as above for the redirection to a text file.

Any ideas as to how I can achieve this ... ?
Thanks so much !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: images = identical ?

Post by fmw42 »

see comparison statistics section on page http://www.imagemagick.org/Usage/compare/#compare
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: images = identical ?

Post by boarders' paradise »

Thanks. I know this page.
But how does that help me to get the output:

"images are identical!"
vs.
"Images are different!"

?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: images = identical ?

Post by fmw42 »

use one measure, eg, rmse and if zero, they are the same and if not zero, then they are different by that amount.

compare -metric rmse image1 image2 null:

The result will be simply returned as a value to the terminal.

Or you can make a test:

comparison=`compare -metric rmse image1 image2 null: 2>&1`
[ "$comparison" = "0 (0)" ] && echo "same" || echo "not same"

or all in one line:

[ "$( compare -metric rmse zelda3.jpg zelda3.jpg null: 2>&1 )" = "0 (0)" ] && echo "same" || echo "not same"
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: images = identical ?

Post by el_supremo »

IM writes the info you need to the standard error output. On windows you need to redirect the output like this:

Code: Select all

compare.exe -metric AE logo: logo_sig.jpg null: 2>metric.txt
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: images = identical ?

Post by boarders' paradise »

thanks so much for helping me.

I looked around in the IM documentation, but I don't understand the red parts:

compare.exe -metric AE logo: logo_sig.jpg null: 2>metric.txt

what does "logo:" mean ?
and "null: 2" ?

I guess "null:" means that no difference-file is created. But what is the "2" afterwards ?

el_supremo, you said that IM writes the info to the standard error output. I think this is the reason, why I couldn't "capture" the screen output.

And thanks to you too fmw42. You said I should use -metric rmse. Is -metric AE (like I wrote in my OP) wrong ?
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: images = identical ?

Post by boarders' paradise »

OK, I played around with that a little bit and see now, that

null:

doesn't produce a difference file. Ok. And with:

Code: Select all

compare -metric AE someimage1.jpg someimage2.png differences.bmp 2>results.txt
... I can create an output file, plus I redirect the output to a text file. I still don't understand, why I have to write 2> instead of just > but apparently that's how it works.

If you can help me understand this a bit, I'd be very thankful ...
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: images = identical ?

Post by boarders' paradise »

The redirection to text file will certainly do it. I was just curious:

I wanted to use FOR /F to parse the output of a command, in this case ImageMagick's compare.exe. But since, as you just said, IM writes to the standard error output, I'm a bit clueless if this is even possible.

for example:

Code: Select all

FOR /F "delims=*" %%i IN ('dir') DO echo %%i
does print the output of the dir command on screen and stores it into the variable %%i.

I wanted to do the same thing for ImageMagick. But this does not work:

Code: Select all

FOR /F %%i in (`compare -metric AE %1 %2 differences.bmp`) DO echo %%i
... because of this "writes to standard error output" -thing, I guess.

Or is there a way, I can store ImageMagick's output into a variable when working in a batch file ?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: images = identical ?

Post by el_supremo »

The standard output is file descriptor 1. The error output is file descriptor 2 (this all originated with Unix).
When you use the redirection symbol ">" you are redirecting the output from file descriptor 1.
The "2>" allows you to redirect the output of file descriptor 2 which is the standard error output.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: images = identical ?

Post by anthony »

One point the -metric AE also allows you to use a -fuzz setting so you can control the count of 'how pixels are' before calssing them as different.

That can be important as pixels could have a small fluxuation based on passed history and conversions the image went through. Even the IM version changes can create slight differences in image that otherwize would be classed as identical.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: images = identical ?

Post by boarders' paradise »

@el_supremo: I see. Thanks for this insight. Redirection works now. Are you familiar a bit with batch files, el_supremo? If yes, could you tell me how I have to use the standard error output in combination with the FOR /F command as I posted above ?

Code: Select all

FOR /F %%i in (`compare -metric AE %1 %2 differences.bmp`) DO echo %%i
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: images = identical ?

Post by boarders' paradise »

@anthony: ah okay, but if I don't use the fuzz setting, ImageMagick reports all pixels, that are not 100% identical, right ?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: images = identical ?

Post by anthony »

boarders' paradise wrote:@anthony: ah okay, but if I don't use the fuzz setting, ImageMagick reports all pixels, that are not 100% identical, right ?
Right.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: images = identical ?

Post by el_supremo »

I haven't been able to find/figure out how to redirect the error output from within a DOS "for" command so you might have to live with something like this:

Code: Select all

compare -metric AE logo: logo_blur.jpg null 2>ae.txt
for /f %%i in (ae.txt) do echo %%i
Note that the % is doubled when it is used in a batch file but if you type those commands on a DOS command line then there must be one % (i.e. - for /f %i in (ae.txt) do echo %i)

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: images = identical ?

Post by boarders' paradise »

Thanks el_supremo. Yes, that's what I've ended up doing in the meanwhile. If you ever come across a solution for "FOR /F in ('command')", then please let me know and post here, even if it's in - let's say a year - ... thanks.

BTW, I couldn't figure out what logo: stands for ?
Post Reply