Page 1 of 1
Return metrics using VBA
Posted: 2014-05-11T10:04:14-07:00
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
Re: Return metrics using VBA
Posted: 2014-05-11T10:35:50-07:00
by fmw42
it goes to standard error. use 2>&1 in unix. I do not know about Windows.
Re: Return metrics using VBA
Posted: 2014-05-11T13:54:32-07:00
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!
Re: Return metrics using VBA
Posted: 2014-05-11T17:15:36-07:00
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")
Re: Return metrics using VBA
Posted: 2014-05-12T00:39:42-07:00
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".
Re: Return metrics using VBA
Posted: 2014-05-15T11:59:42-07:00
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!
Re: Return metrics using VBA
Posted: 2014-05-15T12:04:44-07:00
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.
Re: Return metrics using VBA
Posted: 2014-05-15T12:46:44-07:00
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
Re: Return metrics using VBA
Posted: 2014-05-20T05:19:40-07:00
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!