Use full pathname of image with Convert

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
lalillie
Posts: 3
Joined: 2014-04-08T13:32:19-07:00
Authentication code: 6789

Use full pathname of image with Convert

Post 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
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Use full pathname of image with Convert

Post 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'?
.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: Use full pathname of image with Convert

Post by snibgo »

Try using forward slash / or double backslash \\.
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: Use full pathname of image with Convert

Post 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.
lalillie
Posts: 3
Joined: 2014-04-08T13:32:19-07:00
Authentication code: 6789

Re: Use full pathname of image with Convert

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Use full pathname of image with Convert

Post by snibgo »

I suggest you find the minimum script that shows the problem. Paste that here, between [ code ] and [ /code].
snibgo's IM pages: im.snibgo.com
lalillie
Posts: 3
Joined: 2014-04-08T13:32:19-07:00
Authentication code: 6789

Re: Use full pathname of image with Convert

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Use full pathname of image with Convert

Post 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.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Use full pathname of image with Convert

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply