2017-09-22 124 views
0

以下代碼完美地從命令行完美組合兩個TIFF文件。如何正確地將參數傳遞給Image Magick從PowerShell

​​

然而,當我嘗試使用它在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

+0

FYI:'magick -quiet -compress JPEG file1.tif file2.tif filecombined.tif' is not proper IM 6 or 7 syntax'。在任何操作員或對輸入圖像執行操作的設置之前閱讀輸入圖像。正確的語法是:'magick -quiet file1.tif file2.tif -compress JPEG filecombined.tif'。 IM 7的語法不如IM 6更爲寬容。 – fmw42

+0

猜猜我很幸運能夠使用7.更新了Q&A以適應這些知識。 – Code39

回答

0

把所有的參數Magick到一個數組,並使用該呼叫(&)運算符來執行該命令。

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

這可能不是最有效地利用陣列,但類似的方法是可能的,如果性能是一個問題。

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 
} 
相關問題