I try to manipulate pictures with imageick. On the CLI I use this working fine command:
C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe D:\orG\3.08_10-02_1.jpg -resize 1200x1200 D:\orG\output\3.08_10-02_1.jpg
But I try to that in a powershell script (test.ps1) with this code:
$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg2 = 'D:\orG\3.08_10-02_1.jpg'
$arg3 = '-resize 1200x1200'
$arg4 = 'D:\orG\output\3.08_10-02_1.jpg'
& $CMD $arg2 $arg3 $arg4
Im getting this error: magick.exe: unrecognized option `-resize 1200x1200' at CLI arg 2 @ fatal/magick-cli.c/ProcessCommandOptions/659.
I have also tried:
$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg1 = 'convert'
$arg2 = 'D:\orG\3.08_10-02_1.jpg'
$arg3 = '-resize 1200x1200'
$arg4 = 'D:\orG\output\3.08_10-02_1.jpg'
& $CMD $arg1 $arg2 $arg3 $arg4
But the error is similar: convert: unrecognized option `-resize 1200x1200' @ error/convert.c/ConvertImageCommand/2686.
If I remove the option "-resize 1200x1200". There is now error message and the command is writing the file (without resize) to the output folder. So I think, that I have a problem with my option, but I can't find it. I have invested several hours and als tried with the @-symbol for arguments, without success.
You have to separate argument name and value, otherwise -resize 1200x1200
will be passed as a single token, but native executables usually expect argument name and value to be separate tokens (unless the syntax is without whitespace, e. g. -name:value
).
$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg1 = 'convert'
$arg2 = 'D:\orG\3.08_10-02_1.jpg'
$arg3 = '-resize', '1200x1200'
# |<-- inserted comma here
$arg4 = 'D:\orG\output\3.08_10-02_1.jpg'
& $CMD $arg1 $arg2 $arg3 $arg4
This turns $arg3
into an array of two strings, which PowerShell passes as separate argument tokens. This is also called splatting and is only done when calling native executables (for PowerShell commands you have to use the @
splatting operator).
Another, more concise way:
$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
# Create an array of arguments
$imArgs = @(
'convert'
'D:\orG\3.08_10-02_1.jpg'
'-resize', '1200x1200'
'D:\orG\output\3.08_10-02_1.jpg'
)
# Call native command, which splats argument array.
& $CMD $imArgs
# Alternatively you can be explicit about splatting:
& $CMD @imArgs
Make sure you don't name the variable $args
which is a reserved PowerShell variable.
As an aside, if you want to be able to call ImageMagick without having to hardcode its path in all of your scripts, add the installation directory of ImageMagick ("C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64") to the PATH
environment variable.
You could then call ImageMagick simply by specifying the name of its executable:
magick @imArgs