Page 1 of 2
images = identical ?
Posted: 2008-12-03T12:41:32-07:00
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 !
Re: images = identical ?
Posted: 2008-12-03T12:53:50-07:00
by fmw42
Re: images = identical ?
Posted: 2008-12-03T13:18:08-07:00
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!"
?
Re: images = identical ?
Posted: 2008-12-03T13:50:54-07:00
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"
Re: images = identical ?
Posted: 2008-12-03T14:13:35-07:00
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
Re: images = identical ?
Posted: 2008-12-03T15:52:16-07:00
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 ?
Re: images = identical ?
Posted: 2008-12-03T16:00:53-07:00
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 ...
Re: images = identical ?
Posted: 2008-12-03T16:12:00-07:00
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 ?
Re: images = identical ?
Posted: 2008-12-03T19:01:50-07:00
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
Re: images = identical ?
Posted: 2008-12-03T19:05:19-07:00
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.
Re: images = identical ?
Posted: 2008-12-03T20:30:41-07:00
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
Re: images = identical ?
Posted: 2008-12-03T20:33:53-07:00
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 ?
Re: images = identical ?
Posted: 2008-12-03T22:28:01-07:00
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.
Re: images = identical ?
Posted: 2008-12-04T08:58:11-07:00
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
Re: images = identical ?
Posted: 2008-12-04T12:16:11-07:00
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 ?