この記事は2008/01/29にわんくまブログで書いたものです。
昨年、デジタル一眼レフを購入したのですが、それをきっかけに息子のスポ少(ミニバス)の写真を撮る機会が増えました。
チームメンバーの方から写真がほしいと言われるので、専用サイトへアップするのですが元画像のままだとサイズが大きすぎるし、枚数は多いしで大変なのです。
ということでリサイズする関数を作ってみました。(jpeg専用です)
[void][reflection.assembly]::LoadWithPartialName("System.Drawing") [void][reflection.assembly]::LoadWithPartialName("System.Drawing.Imaging") #=============================================================================== # Resize-Image: 画像ファイルをリサイズする(jpegのみ) # # パラメータ # -srcFile : リサイズ元画像ファイルパス # -dstFile : リサイズ後画像ファイルパス # -width : リサイズ後の幅 # -height : リサイズ後の高さ # -quality : 品質 # Default 既定の補間モード # Low 低品質補間 # High 高品質補間 # NearestNeighbor 最近傍補間 # Bilinear 双一次補間 # Bicubic 双三次補間 # HighQualityBilinear 高品質双一次補間 # HighQualityBicubic 高品質双三次補間 # 使用例 # PS > Resize-Image "C:\Work\AAA.jpg" "C:\Work\BBB.jpg" 600 480 "HighQualityBicubic" # # copyright HIRO's.NET(http://hiros-dot.net/) #=============================================================================== function global:Resize-Image { Param([string]$srcFile, [string]$dstFile, [int]$width=0, [int]$height=0, [string]$quality="Default") #元画像ファイルの存在確認 if ( (Test-Path $srcFile) -eq $False ) { Write-Host "リサイズ元画像が存在しません。パスを確認してください!!" return } #元画像の読み込み $srcImg = New-Object System.Drawing.Bitmap($srcFile) $newWidth = 0 $newHeight = 0 #受け取った引数から、新しいサイズを決定する if ( $width -gt 0 -and $height -eq 0 ) { $newWidth = $width $newHeight = $srcImg.Height / ($srcImg.Width / $newWidth) } elseif ( $height -gt 0 -and $width -eq 0 ) { $newHeight = $height $newWidth = $srcImg.Width / ($srcImg.Height / $newHeight) } elseif ( $width -gt 0 -and $height -gt 0 ) { $newWidth = $width $newHeight = $height } else { Write-Host "画像サイズを指定してください" return } $dstImg = New-Object System.Drawing.Bitmap($srcImg, $newWidth, $newHeight) $g = [System.Drawing.Graphics]::FromImage($dstImg) #画像品質の設定 switch ($quality) { "Default" #既定の補間モード {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Default} "Low" #低品質補間 {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Low} "High" #高品質補間 {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::High} "NearestNeighbor" #最近傍補間 {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::NearestNeighbor} "Bilinear" #双一次補間 {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Bilinear} "Bicubic" #双三次補間 {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Bicubic} "HighQualityBilinear" #高品質双一次補間 {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBilinear} "HighQualityBicubic" #高品質双三次補間 {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic} default {$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Default} } #リサイズ画像を作成し保存する $g.DrawImage($srcImg, 0, 0, $newWidth, $newHeight) $dstImg.Save($dstFile, [System.Drawing.Imaging.ImageFormat]::Jpeg) #オブジェクトの破棄 $srcImg.Dispose() $dstImg.Dispose() }
使用方法
Resize-Image "リサイズ元画像ファイルのパス" "リサイズ後の画像保存先パス" 幅 高さ 品質
です。
品質には文字列を指定するのですが、指定できる値は下記のとおりです。
ポイント
1.画像ファイルを扱うので、System.Drawingは必須です。 先頭でアセンブリをロードします。
2.縦横比率を維持する
幅と高さを適切に指定すれば、見た目にもキチンとリサイズされますが、いちいち縦横比を計算してから指定するのは面倒です。 ということで幅または高さのどちらか一方を指定すると、縦横比率を計算したサイズを決定するようにしてあります。
関数内の"#受け取った引数から、新しいサイズを決定する "のコメント以下のif文がその計算を行っている箇所です。
ということでもう1つの使用方法ですが
Resize-Image "リサイズ元画像ファイルのパス" "リサイズ後の画像保存先パス" -width 幅 -quality 品質
または
Resize-Image "リサイズ元画像ファイルのパス" "リサイズ後の画像保存先パス" -height 高さ -quality 品質
のようにします。
これで縦横比が維持された画像が作成されます。
3.画像品質
画像品質を指定するにはInterpolationModeを使用します。
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail