Page 1 of 1
Use full pathname of image with Convert
Posted: 2014-04-08T14:31:57-07:00
by lalillie
I have started to work with VB script and ImageMagick recently. I wrote a short VB script to convert PNG images to TIF. It worked fine when the images were in the same directory as the script and no pathnames were included. Now we need to run the script with import and archive directory parameters. The script no longer does the conversion and the issue appears to be with the pathnames. Example of the final command called: "C:\Program Files\ImageMagick\Convert" -adjoin "C:\Images\484912282013141D_0_FRONT.PNG" "C:\Images\484912282013141D_1_BACK.PNG" "C:\Images\484912282013141C_0_FRONT.PNG" -threshold 60% -density 118 "C:\Images\484912282013141.TIF"
When this line is pasted in a command window, it works. But it doesn't work when the script is run. The script only works if no pathnames are included. Does anyone have an idea about how I can get this to work? In the script, that example is assigned to cmdLine and executed with "oshell.Run "cmd /C " & cmdline, 0".
Appreciate any help I can get.
Laura
Re: Use full pathname of image with Convert
Posted: 2014-04-08T14:41:07-07:00
by dlemstra
Are you getting any error messages? 'It doesn't work' does not tell us that much. Does the path to your image include spaces or is the path really 'C:\Images'?
Re: Use full pathname of image with Convert
Posted: 2014-04-08T14:50:32-07:00
by snibgo
Try using forward slash / or double backslash \\.
Re: Use full pathname of image with Convert
Posted: 2014-04-08T15:52:14-07:00
by fmw42
In principle for proper use of IM 6, the images should be read before applying an operator. Do you really want -adjoin or do you really mean -append.
Re: Use full pathname of image with Convert
Posted: 2014-04-08T20:00:25-07:00
by lalillie
dlemstra wrote:Are you getting any error messages? 'It doesn't work' does not tell us that much. Does the path to your image include spaces or is the path really 'C:\Images'?
There are no errors. The TIF image file is just not created. The path may or may not include spaces so each filename is surrounded by double quotes. The example path I provided is one I am using for testing.
fmw42 wrote:In principle for proper use of IM 6, the images should be read before applying an operator. Do you really want -adjoin or do you really mean -append.
It is necessary to use -adjoin to create a multi-image Tif file. I don't believe there is any problem with the settings used with the Convert command. The example I presented creates the converted image file when run by itself in a command window. It creates the converted image file from within the script when the script is run from the image directory and no pathnames are included. It does not create the converted image file when pathnames are used in the script.
snibgo wrote:Try using forward slash / or double backslash \\
Using either forward slash or double backslash gives the same result. The converted image file is created when run directly from the command prompt but not when the script is run.
Re: Use full pathname of image with Convert
Posted: 2014-04-08T21:37:22-07:00
by snibgo
I suggest you find the minimum script that shows the problem. Paste that here, between [ code ] and [ /code].
Re: Use full pathname of image with Convert
Posted: 2014-04-09T06:33:42-07:00
by lalillie
Test.tif is created. Quoted_test.tif is not.
Code: Select all
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = WScript.CreateObject("WSCript.shell")
IMConvert = oshell.ExpandEnvironmentStrings("%PROGRAMFILES%") & "\ImageMagicK\Convert"
progFolder = objFSO.GetParentFolderName(Wscript.ScriptFullName)
importFolder = "C:\Images\"
imgDocs = "img_1.PNG"
imageArg = " -threshold 60% -density 118 "
rootFilename = "quoted_test"
imgPNGs = Chr(34) & importFolder & imgDocs & Chr(34)
cmdline = Chr(34) & IMConvert & Chr(34) & " " & imgPNGs & imageArg & Chr(34) & importFolder & rootFilename & ".TIF" & Chr(34)
oshell.run "cmd /C " & cmdline, 0
WScript.Echo "Quoted Test" & Chr(10) & cmdLine
rootFilename = "test"
imgPNGs = importFolder & imgDocs
cmdline = Chr(34) & IMConvert & Chr(34) & " " & imgPNGs & imageArg & importFolder & rootFilename & ".TIF"
oshell.run "cmd /C " & cmdline, 0
WScript.Echo "Unquoted Test" & Chr(10) & cmdLine
Re: Use full pathname of image with Convert
Posted: 2014-04-09T17:32:40-07:00
by snibgo
I dunno. It isn't an ImageMagick problem. If you use exec instead of run, you can echo the stderr from each command. Some experimentation shows that if you don't quote IMConvert, or you don't quote the output file, it works.
Re: Use full pathname of image with Convert
Posted: 2014-04-09T19:00:57-07:00
by snibgo
Cracked it. See "cmd /?" for how it processes quotes. It may "strip the leading character and remove the last quote character on the command line".
Alternative solutions:
1. Add /S to cmd, eg
Code: Select all
Set objExecObject = oshell.exec ("cmd /S /C " & cmdline)
2. There is no need to use cmd at all. cmd is a shell, but you don't need it. Eg:
Code: Select all
Set objExecObject = oshell.exec (cmdline)
Solution 2 is faster and more elegant.