如何从PowerShell正确地将参数传递给ImageMagick?

时间:2017-09-22 23:26:59

标签: powershell imagemagick

以下代码可以从命令行完美地组合两个TIFF文件。

magick -quiet file1.tif file2.tif -compress JPEG filecombined.tif

但是,当我尝试在PowerShell中使用它时,我从Magick收到许多错误,表明它没有获得正确的参数。我的PowerShell代码如下所示。

$InputFiles = 'files1.tif file2.tif'
$DestinationFile = 'filecombined.tif'
$magick -quiet $InputFiles -compress JPEG $DestinationFile

这给了我错误,指出它无法找到输入文件,并且消息表明它认为它是一个文件名而不是两个。在PowerShell v4中,我能够通过引用每个名称来使其工作。不知道为什么这有帮助,但名字没有空格。但是,我不得不升级到v5,这种方法破了。

我尝试使用临时文件来存储输入文件名,但这只会导致不同的错误。

$InputFiles = 'files1.tif file2.tif'
$InputFiles | Out-File list.tmp
$DestinationFile = 'filecombined.tif'
$magick -quiet '@list.tmp' -compress JPEG $DestinationFile

magick.exe:无法打开图像'@z:ÿþz

2 个答案:

答案 0 :(得分:1)

我在几个文件夹中收集了大量EPS图像,我必须转换为PNG。我测试了许多图像转换程序,但大多数都无法处理Vector到Raster的递归转换而没有阻塞(处理有限数量的文件后大多数显示错误。有些无法递归转换通过许多子文件夹)。我终于发现了以下Powershell脚本,它解决了我的问题并简单地对许多文件和文件夹进行了递归转换。您可以修改该文件以执行所需的任何ImageMagick功能。

此脚本还显示已处理的所有文件的运行列表,并根据需要创建日志文件。 玩得开心。

# Powershell script to recursively convert image formats
# Tested with ImageMagick v7
# Configuration
$srcfolder = "C:\Program Files\ImageMagick\rktest"
# Make sure the destination folder exists
$destfolder = "C:\Program Files\ImageMagick\rktest\converted"
#This ps1 file will add copy files to designated folder
#Do NOT use Mogrify or the original images will be deleted
$im_convert_exe = "convert -density 300"
# with VECTOR files the density setting should come BEFORE the vector format
# is specified or the image will be blurry.
# for example - for vector files place -density option immediately after the convert.exe
# command in the im_convert_exe definition.  This way it will be set before any 
# vector format is specified.
# change src_filter to the format of the source files
$src_filter = "*.eps"

# change dest_ext to the format of the destination files
$dest_ext = "png"
# the colorspace option prevents RGB errors
# If your image is black and white - the threshold option can be used to make sure output is only black and white.
# for example, add the "-threshold 40%" option to assure there are no grays in the output
# To process RGB images in IM7, set colorspace to RGB "-colorspace sRGB"
$options = "-colorspace gray -threshold 40% -depth 8 -alpha off"
$logfile = "C:\temp\convert.log"
$fp = New-Item -ItemType file $logfile -force
$count=0
foreach ($srcitem in $(Get-ChildItem $srcfolder -include $src_filter -recurse))
{
    $srcname = $srcitem.fullname

    # Construct the filename and filepath for the output
    $partial = $srcitem.FullName.Substring( $srcfolder.Length )
    $destname = $destfolder + $partial
    $destname= [System.IO.Path]::ChangeExtension( $destname , $dest_ext )
    $destpath = [System.IO.Path]::GetDirectoryName( $destname )

    # Create the destination path if it does not exist
    if (-not (test-path $destpath))
    {
        New-Item $destpath -type directory | Out-Null
    }

    # Perform the conversion by calling an external tool
    $cmdline =  $im_convert_exe + " `"" + $srcname  + "`"" + $options + " `"" + $destname + "`" " 
    #echo $cmdline
    invoke-expression -command $cmdline

    # Get information about the output file    
    $destitem = Get-item $destname

    # Show and record information comparing the input and output files
    $info = [string]::Format( "{0} `t {1} `t {2} `t {3} `t {4} `t {5}", $count, 
    $partial, $srcname, $destname, $srcitem.Length ,  $destitem.Length)
    echo $info
    Add-Content $fp $info

    $count=$count+1
} 

答案 1 :(得分:0)

将Magick的所有参数放入数组并使用call(&)运算符执行命令。

$MagickParameters = @( '-quiet' )
$MagickParameters += 'file1.tif'
$MagickParameters += 'file2.tif'
$MagickParameters += @( '-compress', 'JPEG' )
$MagickParameters += 'filecombined.tif'
&'magick' $MagickParameters

这可能不是数组的最有效使用,但如果考虑性能,则可以采用类似的方法。