Page 1 of 1
compare in AppleScript
Posted: 2017-03-13T07:22:42-07:00
by karabugla
Hi,
I´m new to ImageMagick and Scripting in general. I don´t know if this is the right place to ask for help - if not, please feel free to move the thread or delete it.
I am running IM 6.9.7-10_10 on MacOS Sierra, installed via MacPort.
I am currently trying to write an applescript that allows me to compare two images and if they differ copy the 2nd image into the clipboard. My code looks like this at the moment:
Code: Select all
set x to do shell script "/opt/local/bin/compare -metric AE ~/Desktop/TEST1.png ~/Desktop/TEST2.png ~/Desktop/null:"
if x > 0 then
display dialog "copy to clipboard"
end if
Running this script always results in a syntax error unless the two images are identical. When I run
Code: Select all
compare -metric AE TEST1.png TEST2.png null:
in a Terminal window I get the score and no error message is displayed.
The question is why the syntax error and how can I get around it? Since I´m only interested in if there is a difference (and not what that difference is) I don´t need a result image. Is it possible to look at the statistic exclusively and avoid creating the result image?
Any help appreciated.
Best regards,
kara
Re: compare in AppleScript
Posted: 2017-03-13T08:43:29-07:00
by dlemstra
Why is your last argument "null"? Do you mean "null:" instead?
Re: compare in AppleScript
Posted: 2017-03-13T08:52:47-07:00
by karabugla
Yes, my bad...corrected.
Re: compare in AppleScript
Posted: 2017-03-13T09:36:04-07:00
by dlemstra
The exit code from compare will be non zero when the images are not identical. And that will cause an error. You prob want to ignore the exit code. No idea hoe to do that in applescript though.
Re: compare in AppleScript
Posted: 2017-03-13T11:06:41-07:00
by snibgo
I know nothing about applescript.
karabugla wrote:Running this script always results in a syntax error unless the two images are identical.
You don't say what the error message is.
You have a line:
Code: Select all
display dialog "copy to clipboard"
Is this an Apple application? IM has a program called "display", so perhaps that is the confusion.
You output to "~/Desktop/null:". That might also be a problem. "null:" would mean don't create an output file anywhere, so a directory would be superfluous and perhaps an error.
Re: compare in AppleScript
Posted: 2017-03-13T15:22:37-07:00
by fmw42
IM 6.9.8.0 Q16 Mac OS SnowLeopard
I ran the following Applescript:
Code: Select all
set x to do shell script "/usr/local/bin/compare -metric rmse ~/Desktop/M50_1001_01_02_160731_101930.png ~/Desktop/W50E_20160807_Alive_Womens_50_PLUS_with_border_warped.png null:"
return x
It returns:
error "8704.01 (0.132815)" number 1
There is no real error. It is getting that because IM returns an error code of 1 if the value is anything but a perfect match, for which it returns an error code of 0. That is just the way IM works.
Any way, the value returned is the correct result, namely,
8704.01 (0.132815)
The first number is the rmse difference in raw values in the range 0 to 65535 (because my IM is Q16). The second number in parens is the value in the range 0 to 1.
If I change the metric to AE, then you only get one value back since it returns only the number of pixels that are different.
Code: Select all
set x to do shell script "/usr/local/bin/compare -metric ae ~/Desktop/M50_1001_01_02_160731_101930.png ~/Desktop/W50E_20160807_Alive_Womens_50_PLUS_with_border_warped.png null:"
return x
Returns: error "34919" number 1
If I do a perfect match, then
Code: Select all
set x to do shell script "/usr/local/bin/compare -metric rmse ~/Desktop/M50_1001_01_02_160731_101930.png ~/Desktop/M50_1001_01_02_160731_101930.png null: 2>&1"
return x
Returns: "0 (0)"
Or for metric AE
Code: Select all
set x to do shell script "/usr/local/bin/compare -metric ae ~/Desktop/M50_1001_01_02_160731_101930.png ~/Desktop/M50_1001_01_02_160731_101930.png null: 2>&1"
return x
Returns: "0"
So to tell if the script finds a perfect match, just test for the value 0 from the information returned.
Re: compare in AppleScript
Posted: 2017-03-16T03:23:25-07:00
by karabugla
My apologies - I have been travelling the last 2 days.
@snibgo: The error message was the result that I also got running compare in terminal. The 'display dialog' line was just meant as a placeholder for the later code.
@fmw42: Thanks for your detailed analysis/explanation. I´ll work with that and report back!