縦横を指定して画像をリサイズするバッチ(ffmpeg使用)
PowerShellとパッチで画像をリサイズする。(複数選択可)
ffmpegを使用してPowerShellで画像をリサイズする方法を紹介します。ffmpegは動画や音声の変換に使われることが多いですが、画像のリサイズにも利用できます。
以下のスクリプトは、バッチをshell:sendtoに置き選択することで複数の画像をリサイズし、出力画像名フォルダに画像ザイズを付加した名前で保存します。リサイズ後の画像の幅と高さを指定できますがどちらか一方でも構いません。画像の比率に合わせて出力されます。
ResizeImages.ps1
# ResizeImages.ps1
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
$ffmpegPath = "E:\******\Soft\ffmpeg\bin\ffmpeg.exe"
if (-not (Test-Path $ffmpegPath)) {
[System.Windows.Forms.MessageBox]::Show("ffmpeg が見つかりません:`n$ffmpegPath","エラー",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error)
exit 1
}
# 横・縦サイズ入力
$width = [Microsoft.VisualBasic.Interaction]::InputBox("横サイズ(ピクセル)0なら自動計算","横サイズ指定","0")
if ([string]::IsNullOrWhiteSpace($width)) { exit 0 } # キャンセル時終了
$height = [Microsoft.VisualBasic.Interaction]::InputBox("縦サイズ(ピクセル)0なら自動計算","縦サイズ指定","0")
if ([string]::IsNullOrWhiteSpace($height)) { exit 0 } # キャンセル時終了
if ($width -notmatch '^\d+$' -or $height -notmatch '^\d+$') {
[System.Windows.Forms.MessageBox]::Show("サイズは整数で入力してください。","エラー",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error)
exit 1
}
$scaleWidth = if ($width -eq 0) { -1 } else { $width }
$scaleHeight = if ($height -eq 0) { -1 } else { $height }
# フォルダ名用
$folderWidth = if ($width -eq 0) { "Auto" } else { $width }
$folderHeight = if ($height -eq 0) { "Auto" } else { $height }
$folderName = "${folderWidth}x${folderHeight}"
$files = $args
if ($files.Count -eq 0) {
[System.Windows.Forms.MessageBox]::Show("ファイルを選択してから実行してください。","情報",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information)
exit 0
}
foreach ($file in $files) {
try {
if (-not (Test-Path $file)) { continue }
$inputFolder = Split-Path -Path $file -Parent
$outputFolder = Join-Path $inputFolder $folderName
if (-not (Test-Path $outputFolder)) { New-Item -ItemType Directory -Path $outputFolder | Out-Null }
$origBase = [System.IO.Path]::GetFileNameWithoutExtension($file)
$baseName = "${folderName}_${origBase}"
$outFile = Join-Path $outputFolder ($baseName + ".png")
# 同名ファイル処理
$counter = 1
$finalOut = $outFile
while (Test-Path $finalOut) {
$finalOut = Join-Path $outputFolder ("{0}({1}).png" -f $baseName, $counter)
$counter++
}
$outFile = $finalOut
# ffmpeg 変換
& $ffmpegPath -y -i $file -vf "scale=${scaleWidth}:${scaleHeight}" $outFile
} catch {
[System.Windows.Forms.MessageBox]::Show("処理中にエラー: `n$file`n$($_.Exception.Message)","エラー",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error)
}
}
[System.Windows.Forms.MessageBox]::Show("変換完了しました。出力フォルダ:`n" + (Join-Path (Split-Path -Path $files[0] -Parent) $folderName),"完了",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information)
このスクリプトを実行するには、ffmpegがインストールされている必要があります。スクリプト内の$ffmpegPath変数を、実際のffmpegのパスに変更してください。
次にshell:sendtoに置くバッチを作ります。
Resize.bat
@echo off
REM SendTo から呼ぶラッパー
REM ResizeImages.ps1は同一フォルダに置く
powershell -NoProfile -ExecutionPolicy Bypass -File %scriptPath% %*
pause
exit /b
このバッチファイルのショートカットをshell:sendtoフォルダに保存します。これで、エクスプローラーで画像ファイルを選択し、右クリックメニューから「送る」→「Resize.bat」を選択することで、画像のリサイズが実行されます。
リサイズ後の画像は、元の画像と同じフォルダ内に、指定したサイズの名前を持つフォルダに保存されます。
注意: ffmpegのバージョンや環境によっては、コマンドのオプションが異なる場合がありますので、必要に応じて調整してください。
コメント
コメントを投稿