2017-09-22 3 views
0

次のコードは、2つのTIFFファイルを結合するコマンドラインから完全に動作します。私はPowerShellでそれを使用しようとすると、PowerShellからImage Magickにパラメータを正しく渡す方法

​​

しかし、私はそれが正しいパラメータを取得していないことを示している魔術から多くのエラーを取得しています。私のPowerShellコードは次のようになります。

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

入力ファイルが見つからないというメッセージが表示され、2つではなく1つのファイル名であるとメッセージが表示されます。 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を開くことができません:

+0

FYI: 'magick -quiet -compress JPEG file1.tif file2.tif filecombined.tif'は適切なIM 6または7構文ではありません。入力画像に作用する操作者または設定の前に入力画像を読み取ります。正しい構文は 'magick -quiet file1.tif file2.tif -compress JPEG filecombined.tif'です。 IM 7はIM 6より文法の方が寛容ではありません。 – fmw42

+0

私はこの知識を受け入れるために7つのQ&Aを使用して幸運だったと思います。 – Code39

答えて

0

ÿþz配列に魔術のすべてのパラメータを入れてコマンドを実行するための呼び出し(&)演算子を使用します。

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

これは、アレイの最も効率的な使用ではない可能性がありますが、パフォーマンスが問題になる場合は同様の方法が可能です。

1

私はPNGに変換しなければならなかったいくつかのフォルダに大きなEPS画像を持っていました。私は多くのImage Conversionプログラムをテストしましたが、ほとんどの場合、チョークを行わずに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 
} 
関連する問題