2011-07-28 10 views
5

フォルダとサブフォルダ内のすべての画像をjpgに変換する必要があります。コマンドファイルでこのプロセスをバッチする必要があります。 GUIツールの針私はスクリプトが必要です。すべての画像をjpgに変換

ImageMagickからmogrify.exeを使用しようとしましたが、convert.exe機能もありますが、どちらも画像を変換することができます。

私は次のスクリプトを書きました:

$rootdir = "E:\Apps\скрипты\temp1\graphics" 
$files = dir -r -i *.png $rootdir 
foreach ($file in $files) {.\mogrify.exe -format png *jpg $file} 

をしかし、それは動作していない、私はそれを実行しようとすると、私はエラーを得た:

:私は次のコードを見つけ持っても

mogrify.exe: unable to open file `*jpg' @ error/png.c/ReadPNGImage/3633. 
mogrify.exe: Improper image header `E:\Apps\скрипты\temp1\graphics\telluric\day\ 
Athens\2011-07-03-17.png' @ error/png.c/ReadPNGImage/3641. 
mogrify.exe: unable to open image `*jpg': Invalid argument @ error/blob.c/OpenBl 
ob/2588. 

[Reflection.Assembly]::LoadWithPartialName('System.Drawing') 

$img=[Drawing.Image]::FromFile("$(cd)\Max.jpg") 
$img.Save("$(cd)\max.gif", 'Gif') 
$img.Dispose() 

どのように私はdirsの木で動作し、jpgにpngとtiffを変換することができますか?

答えて

13

私はここにファイルをアイコンにビットマップファイルを変換すると似たようなやった:私はあなたの要件にそれを適応してJPGにイメージファイルを変換するための機能をテスト

http://sev17.com/2011/07/creating-icons-files/

を:

function ConvertTo-Jpg 
{ 
    [cmdletbinding()] 
    param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path) 

    process{ 
     if ($Path -is [string]) 
     { $Path = get-childitem $Path } 

     $Path | foreach { 
      $image = [System.Drawing.Image]::FromFile($($_.FullName)); 
      $FilePath = [IO.Path]::ChangeExtension($_.FullName, '.jpg'); 
      $image.Save($FilePath, [System.Drawing.Imaging.ImageFormat]::Jpeg); 
      $image.Dispose(); 
     } 
    } 

} 

#Use function: 
#Cd to directory w/ png files 
cd .\bin\pngTest 

#Run ConvertTo-Jpg function 
Get-ChildItem *.png | ConvertTo-Jpg 
+0

お返事ありがとうございます! – Suliman

+0

問題ありません。質問に回答したことを覚えておいてください。 –

+0

いいね。手動文字列の書式設定ではなく、[IO.Path] :: ChangeExtension($ _。FullName、 '.jpg') 'を使用します。 –

0

私はChad Miller's answerに、このブログ記事のJPEGの画質を設定する機能を組み込んでいます。
Benoît Patra's blog: Resize image and preserve ratio with Powershell

# Try uncommenting the following line if you receive errors about a missing assembly 
# [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
function ConvertTo-Jpg 
{ 
    [cmdletbinding()] 
    param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path) 

    process{ 
    $qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality 
    $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1) 

    # Set JPEG quality level here: 0 - 100 (inclusive bounds) 
    $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, 100) 
    $jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'} 

    if ($Path -is [string]) { 
     $Path = get-childitem $Path 
    } 

    $Path | foreach { 
     $image = [System.Drawing.Image]::FromFile($($_.FullName)) 
     $filePath = "{0}\{1}.jpg" -f $($_.DirectoryName), $($_.BaseName) 
     $image.Save($filePath, $jpegCodecInfo, $encoderParams) 
     $image.Dispose() 
    } 
    } 
} 

#Use function: 
# cd to directory with png files 
cd .\bin\pngTest 


#Run ConvertTo-Jpg function 
Get-ChildItem *.png | ConvertTo-Jpg 
+0

アルファチャンネルを持つPNGから逆の色を取得します。どのようにそれを修正するための任意のアイデア? –

+0

@RossPresser私は自分ではわかりませんが、クイック検索がこの[answer](http://stackoverflow.com/q/6513633/603003)につながりました。 System.Drawing.Imageは純粋な.NET APIなので、.NET(C#でもPSでもかまいません)を使用するすべての回答は「翻訳」することができます。 – ComFreek

+0

興味深い。私はちょうど私がimagemagickを使うことができる別のマシンに42イメージをコピーしてしまった。しかし、私はいつか心に留めておきます。 –

関連する問題