Get the value of the comparison in the batch script variable

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?".
Post Reply
AlexLN
Posts: 5
Joined: 2016-01-29T13:20:14-07:00
Authentication code: 1151

Get the value of the comparison in the batch script variable

Post by AlexLN »

Hello everyone

I need to compare the pictures and choose a exactly the same or a little bit modified one.
The approximate degree of similarity is known. But I did not get to take the value of the similarity as a variable. It seems that I'm hopeless noob :?
Please help me with the following piece of .cmd code. What am I doing wrong?

Code: Select all

set difference = 'compare -metric MAE file1.jpg file2.jpg null: 2>&1'
if %difference% LSS 0.003 (echo "Same") else (echo "Different")
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get the value of the comparison in the batch script variable

Post by fmw42 »

What version of Imagemagick and what platform? Looks like Windows? Please always provide this information, since syntax may differ.

I am not a Windows user, but the logic seems correct? What kind of error do you get? Are the two images the same size?

The output from compare is two parts. The first number is a raw value in the range 0 to quantumrange for your compile. The second number in parenthesis is the value in the range 0 to 1. I suspect you need to parse the data to get the MAE value in the range 0 to 1
AlexLN
Posts: 5
Joined: 2016-01-29T13:20:14-07:00
Authentication code: 1151

Re: Get the value of the comparison in the batch script variable

Post by AlexLN »

ImageMagick 6.9.3-2-Q16-x64-dll
Windows7.
Yes, the two images of the same size.
fmw42 wrote:What kind of error do you get?
Well, I have no IDE for batch files, just notepad. So what can i say - .cmd file just don't start working. While a program itself is executed well.

Code: Select all

compare -metric MAE file1.jpg file2.jpg null: 2>&1
Sorry, as I said I'm pretty nooby, i just wondering is there a no ready solution for such applied task as transfer of value of a variable for Windows users, because I did not find it in the manual.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get the value of the comparison in the batch script variable

Post by fmw42 »

look at the output of

Code: Select all

compare -metric MAE file1.jpg file2.jpg null:
in a Windows COMMAND window. Just type or copy and paste. You will see that there are two different numbers listed to the terminal

xxx (yyy)

you want the number in parenthesis. Your if command will not test on two values. You must separate them and test only on the latter.

E.G.

Code: Select all

convert rose: rose1.png
convert rose: -blur 0x1 rose2.png

compare -metric MAE rose1.png rose2.png null:
2029.98 (0.0309755)

You want to extract and test with only 0.0309755.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get the value of the comparison in the batch script variable

Post by fmw42 »

AlexLN
Posts: 5
Joined: 2016-01-29T13:20:14-07:00
Authentication code: 1151

Re: Get the value of the comparison in the batch script variable

Post by AlexLN »

I think the problem is not parsing. I just can not even call this variable.

Code: Select all

set difference = 'compare -metric MAE file1.jpg file2.jpg null: 2>&1'
echo %difference%
I should just give this example from the beginning :?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get the value of the comparison in the batch script variable

Post by fmw42 »

try removing the spaces on each side of =

Sorry I am not a Windows user and do not know batch file scripting.

Also in unix, the quotes should be back-ticks. But you are on Windows, so I do not know how they convert an Imagemagick command to a variable.

See http://www.imagemagick.org/Usage/windows/
AlexLN
Posts: 5
Joined: 2016-01-29T13:20:14-07:00
Authentication code: 1151

Re: Get the value of the comparison in the batch script variable

Post by AlexLN »

I've tried all these options ...
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Get the value of the comparison in the batch script variable

Post by GeeMack »

AlexLN wrote:Sorry, as I said I'm pretty nooby, i just wondering is there a no ready solution for such applied task as transfer of value of a variable for Windows users, because I did not find it in the manual.
Getting the output of a command into a variable in Windows is not a very simple task. I'm running ImageMagick 6.9.3-1 Q16 x64 on Windows 7 64, and I've made a batch file like the one below that does what you want. Copy and paste this into your text editor (Notepad, etc.) and save it as something like "imcompare.bat".

Code: Select all

@echo off
setlocal

set IMG1=%~1
set IMG2=%~2

for /F "tokens=2 delims=() " %%A in ( 'compare -metric MAE "%IMG1%" "%IMG2%" null: 2^>^&1' ) do (
   if %%A LSS 0.003 (
      echo Same
   ) else (
      echo Different
   )
)
Then run it from the command line like this...

Code: Select all

imcompare.bat image1.jpg image2.jpg
If your IM "compare.exe" command isn't already in the PATH, you may need to use its full path in your script. If the script isn't in your path or in the current directory, you may need to call it by using its full path. If the image files you're comparing aren't in the current working directory, you may need to call them by their full paths.

The actual workings of the batch file may be outside the scope of this forum. You'll probably get more helpful and detailed answers at a forum about using Windows commands and scripts. Maybe find some pointers about passing arguments to a Windows batch file by studying this page, and learn more about using "for" to get the output of a command into a variable by studying this page.
Last edited by GeeMack on 2016-01-29T23:36:46-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get the value of the comparison in the batch script variable

Post by snibgo »

As GeeMack says.

The form:

Code: Select all

set difference = 'compare -metric MAE file1.jpg file2.jpg null: 2>&1'
is (almost) bash syntax, not Windows BAT syntax. Windows BAT needs the "FOR" command.

My pages have many examples of IM in BAT files.
snibgo's IM pages: im.snibgo.com
AlexLN
Posts: 5
Joined: 2016-01-29T13:20:14-07:00
Authentication code: 1151

Re: Get the value of the comparison in the batch script variable

Post by AlexLN »

Thank you to all who responded very friendly community here and you helped me a lot. Especially GeeMask, a special thank you good sir.
Post Reply