Page 1 of 1
Get the value of the comparison in the batch script variable
Posted: 2016-01-29T13:46:20-07:00
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")
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T15:22:18-07:00
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
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T17:27:05-07:00
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.
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T17:35:28-07:00
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.
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T17:36:52-07:00
by fmw42
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T17:55:36-07:00
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
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T18:07:15-07:00
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/
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T18:10:23-07:00
by AlexLN
I've tried all these options ...
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T19:15:41-07:00
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.
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-29T22:50:03-07:00
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.
Re: Get the value of the comparison in the batch script variable
Posted: 2016-01-30T07:23:52-07:00
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.