Return metrics using VBA

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
Mattjc93
Posts: 4
Joined: 2014-05-11T09:57:44-07:00
Authentication code: 6789

Return metrics using VBA

Post by Mattjc93 »

Hi there,

I'm trying to find out a way of returning image metrics from the compare function, using VBA. This works in command line, but I'm unsure as to the syntax in VBA. My current code is as follows:

Set MGImg = CreateObject("ImageMagickObject.MagickImage.1")

Image1 = "C:\....png"
Image2 = "C:\...png"
output = "C:\...txt"

var = MGImg.Compare ("-metric", "ncc", Image1, Image2, "Null:", output)

Any thoughts on how I can capture the output? Any help much appreciated!

Thanks,
Matt
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Return metrics using VBA

Post by fmw42 »

it goes to standard error. use 2>&1 in unix. I do not know about Windows.
Mattjc93
Posts: 4
Joined: 2014-05-11T09:57:44-07:00
Authentication code: 6789

Re: Return metrics using VBA

Post by Mattjc93 »

Thanks so much for the swift response. Unfortunately I'm on Windows, so not sure how gaining the standard error value is done there!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Return metrics using VBA

Post by fmw42 »

Hopefully a Windows user will come to the rescue. In the mean time try

Code: Select all

var = MGImg.Compare ("-metric", "ncc", Image1, Image2, "null: 2>&1", output)
Not sure you want "output" since the output is null:

so try

Code: Select all

var = MGImg.Compare ("-metric", "ncc", Image1, Image2, "null: 2>&1")
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Return metrics using VBA

Post by snibgo »

I don't use VB, but two possibilities occur to me:

1. Use Exec() and read the stdout.

2. Use "convert" with "-format %[distortion]", which sends output to stdout, instead of "compare".
snibgo's IM pages: im.snibgo.com
Mattjc93
Posts: 4
Joined: 2014-05-11T09:57:44-07:00
Authentication code: 6789

Re: Return metrics using VBA

Post by Mattjc93 »

Thanks for all the responses all. Frustratingly tried them all, and still having no luck with this, so may look into another way of completing the image comparison process - have a feeling it may not be possible through VBA!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Return metrics using VBA

Post by fmw42 »

Did you try snibgo's method 2 above? What was your exact command? Perhaps there is a syntax error. You might post the commands you tried.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Return metrics using VBA

Post by snibgo »

Above, where I wrote "1. Use Exec() and read the stdout" of course I really meant "1. Use Exec() and read the stderr". Sorry.

The exec and stderr method works fine as a VBS script, eg:

Code: Select all

Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")

Set objExecObject = oShell.Exec("c:\im\ImageMagick-6.8.8-7-Q16\compare -metric RMSE a.png b.png NULL:")
If not objExecObject.StdErr.AtEndOfStream then ShowError (objExecObject.StdErr)

Set oShell = Nothing


Sub ShowError (err)
  Dim strText

  Do While Not err.AtEndOfStream
    strText = err.ReadLine()
    Wscript.Echo strText
  Loop

End Sub
snibgo's IM pages: im.snibgo.com
Mattjc93
Posts: 4
Joined: 2014-05-11T09:57:44-07:00
Authentication code: 6789

Re: Return metrics using VBA

Post by Mattjc93 »

Thanks for all the advice! I had trouble with snigbo's example, as it got stuck on the second line and came up with the error message "424 object required".

Couldn't get round that one, so I actually wound up running it as follows:

Public Sub Test()

Shell ("cmd /c >C:...test.txt ""C:\Program Files (x86)\ImageMagick-6.8.9-Q16\compare.exe"" -metric ncc C:\Users\....png C:\.....png NULL: 2>&1")

End Sub

Not the most elegant solution, but did the trick!

Edit: actually scrap that! I think instead of outputting to a file, reading the stderr is a much better long term solution. Re: snigbo's example, I managed to get round the object error by addomg "wshom.ocx" as a reference. However, I'm getting the same error on the "Wscript.echo strText" line... Completely stuck with that one!
Post Reply