Brad
Brad

Reputation: 15879

Calling ImageMagick COM from VBScript

I have successfully used the following command line to create a new GIF image with some simple text

convert -size 100x100 -font arial label:blah output.gif

I would like to achieve the same result using the COM interface from VBScript, but have not been successful in passing in the same parameters in. Here is my code...

Dim img
Set img = CreateObject("ImageMagickObject.MagickImage.1")

Dim fnm(6)
fnm(0) = "-size"
fnm(1) = "100x100"
fnm(2) = "-font"
fnm(3) = "arial"
fnm(4) = "-label"
fnm(5) = "blah"
fnm(6) = "C:\temp\example.gif"

retval = img.convert(fnm)

wscript.echo("retval " & retval)

I execute this code as follows

cscript example.vbs

and the output I get is this, and no GIF file is created...

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Version: ImageMagick 6.7.4-3 2011-12-24 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

Usage: cscript.exe [options ...] file [ [options ...] file ...] [options ...] file

I'm on Windows7 using ImageMagick-6.7.4-Q16

Does anyone know what I am doing wrong?

[Edit]

In response to Ekkehard's answer I have also tried not using an array. This code will create an output image, but the label is not applied. I also have to pass in a white image as an input as it will not work without one.

msgs = img.convert("C:\temp\blank.gif", "-size", "100x100", "-font", "arial", "label:", "blah", "C:\temp\example.gif")

Upvotes: 0

Views: 6786

Answers (3)

Brad
Brad

Reputation: 15879

In the end I gave up trying to get label to work when using the COM+ API and have a solution that uses -draw instead. Ekkehard was right about not using an Array, and instead use individual arguments as shown below.

msgs = img.convert( "c:\temp\blank.gif", "-font", "arial", "-pointsize", "36", "-gravity", "center", "-fill", "blue", "-draw", "text 0,0 'blah'", "c:\temp\example.gif")

Upvotes: 2

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

In the folder [WhereEver]\ImageMagick-6.7.4-Q16\ImageMagickObject\Tests you'll find two sample scripts (ArrayTest.vbs, SimpleTest.vbs). The most important info from the second:

' The argument list is exactly the same as the utility programs
' as a list of strings. In fact you should just be able to
' copy and past - do simple editing and it will work. See the
' other samples for more elaborate command sequences and the
' documentation for the utility programs for more details.
'
msgs = img.Convert("logo:","-format","%m,%h,%w","info:")

So passing all params in one array is as wrong as attempting to set properties. I used

  Dim sSrcFSpec : sSrcFSpec = "..\data\logo.jpg"
  Dim sDstFSpec : sDstFSpec = "..\data\logo.png"
  If goFS.FileExists(sDstFSpec) Then goFS.DeleteFile sDstFSpec
  CreateObject("ImageMagickObject.MagickImage.1").convert "-verbose", sSrcFSpec, sDstFSpec
  If Not goFS.FileExists(sDstFSpec) Then WScript.Echo "Failure!"

output:

..\data\logo.jpg JPEG 123x118 123x118+0+0 8-bit DirectClass 16.2KB 0.000u 0:00.006
..\data\logo.jpg=>..\data\logo.png JPEG 123x118 123x118+0+0 8-bit DirectClass 0.030u 0:00.
083

which proves that you'll get results if you pass reasonable arguments. I tried something similar to your test code -

convert -verbose -size 100x100 -font arial -label blah ..\data\example.gif

and got:

convert.exe: missing an image filename `..\data\example.gif' @ error/convert.c/ConvertImag
eCommand/3016.

Knowing nothing about ImageMagick, I can only assume that such args assume an existing input file.

So do your COM testing when you have verified the parameters from the command line.

Upvotes: 1

Nilpo
Nilpo

Reputation: 4816

A COM interface is going to expose a series of properties and methods for controlling an object. You wouldn't instantiate it by passing an array of values. In most cases, you would set each property individually. It might look something this (purely an example, I don't know if these are real properties for this object):

Dim img
Set img = CreateObject("ImageMagickObject.MagickImage.1")

img.sizeX = 100
img.sizeY = 100
img.font = "Arial"
img.label = "blah"
retval = img.convert("C:\temp\example.gif")

WScript.echo("retval " & retval)

An object browser or type library viewer can be used to examine the COM object's properties and methods. My favorite is TLViewer.

Upvotes: 0

Related Questions