compare in AppleScript

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
karabugla
Posts: 3
Joined: 2017-03-13T07:03:41-07:00
Authentication code: 1151

compare in AppleScript

Post 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
Last edited by karabugla on 2017-03-13T08:52:33-07:00, edited 1 time in total.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: compare in AppleScript

Post by dlemstra »

Why is your last argument "null"? Do you mean "null:" instead?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
karabugla
Posts: 3
Joined: 2017-03-13T07:03:41-07:00
Authentication code: 1151

Re: compare in AppleScript

Post by karabugla »

Yes, my bad...corrected.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: compare in AppleScript

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: compare in AppleScript

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compare in AppleScript

Post 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.
karabugla
Posts: 3
Joined: 2017-03-13T07:03:41-07:00
Authentication code: 1151

Re: compare in AppleScript

Post 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!
Post Reply