やりたい事
- jpgやpngなど画像ファイルをリサイズしたい
- 画像の回転もさせたい
- 複数画像に対し処理させたい
- 別フォルダへ画像を出力 or 上書きしたい
PowerShell スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-Image{ | |
<# | |
.SYNOPSIS | |
Resize and Rotate image file. | |
.DESCRIPTION | |
Image resizing and rotating cmdlets. | |
.PARAMETER Source | |
Specifies an source image file. | |
.PARAMETER Destination | |
Specifies a destination folder. | |
.PARAMETER AspectRatio | |
Specifies a AspectRatio(ValidateSet). | |
WidthFixed : Fixed width pixel and Height pixel is auto caluculate. | |
HeightFixed : Fixed height pixel and Width pixel is auto caluculate. | |
Free : No AspectRatio. | |
.PARAMETER WidthPx | |
Specifies a width of image in pixel. | |
.PARAMETER HeightPx | |
Specifies a height of image in pixel. | |
.PARAMETER Rotate | |
Specifies a Rotate. | |
RotateNoneFlipNone : 0. | |
Rotate90FlipNone : 90. | |
Rotate180FlipNone : 180. | |
Rotate270FlipNone : 270. | |
.PARAMETER Overwrite | |
Specifies a destination exist then overwrite it without prompt. | |
.EXAMPLE | |
Convert-ImageResize -Source "C:\temp2\aaa.jpg", "C:\temp2\bbb.jpg" -Verbose | |
.EXAMPLE | |
Convert-ImageResize -Source C:\temp2\aaa.jpg -Destination c:\temp -AspectRatio WidthFixed -WidthPx 800 -ImageFormat Jpeg -Rotate RotateNoneFlipNone -Verbose | |
.EXAMPLE | |
$source = "C:\source" | |
$destination = "C:\destination" | |
$width = 1600 | |
$height = 600 | |
gci "$source\*.jpg" | foreach{ | |
$img = New-Object System.Drawing.Bitmap($_.FullName) | |
if($img.Width -gt $width){ | |
$img.Dispose() | |
Convert-ImageResize -Source $_.fullName ` | |
-Destination $destination ` | |
-AspectRatio WidthFixed -WidthPx $width -HeightPx $height ` | |
-Rotate Rotate90FlipNone ` | |
-Overwrite ` | |
-Verbose | |
} | |
else{ | |
$img.Dispose() | |
} | |
} | |
.NOTES | |
Author: Satoru Nasu | |
Blog : http://nasunoblogb.blogspot.com/ | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True,Position=1)] | |
[string[]]$Source, | |
[string]$Destination = $("$env:USERPROFILE\Documents"), | |
[ValidateSet("WidthFixed", "HeightFixed", "Free")] | |
[string]$AspectRatio = "WidthFixed", | |
[Int]$WidthPx = "1600", | |
[Int]$HeightPx, | |
[ValidateSet("RotateNoneFlipNone","Rotate90FlipNone","Rotate180FlipNone","Rotate270FlipNone")] | |
[string]$Rotate = "RotateNoneFlipNone", | |
[switch]$Overwrite | |
) | |
Begin{ | |
# アセンブリの読み込み | |
Add-Type -AssemblyName System.Drawing | |
# or | |
#[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | |
} | |
process{ | |
# 出力先フォルダーをチェック | |
if((Test-Path($Destination)) -eq $false){ | |
New-Item $Destination -ItemType Directory | Out-Null | |
} | |
$Source | foreach{ | |
# ファイル名を取得 | |
$name = (Get-Item -Path $_).Name | |
# 拡張子を取得 | |
$ext = ((Get-Item -Path $_).Extension).ToLower() | |
# 画像ファイルの読み込み | |
$image = New-Object System.Drawing.Bitmap($_) | |
# アスペクト比の指定から画像の高さ(px)と幅(px)を指定 | |
$oldWidth = $image.Width | |
$oldHeight = $image.Height | |
if($AspectRatio -eq "WidthFixed"){ | |
$w = $WidthPx | |
$h = $image.Height/($oldWidth / $w) | |
} | |
elseif($AspectRatio -eq "HeightFixed"){ | |
$h = $HeightPx | |
$w = $image.Width/($oldHeight / $h) | |
} | |
else{ | |
if([int]$Width -and [int]$Height){ | |
$w = $WidthPx | |
$h = $HeightPx | |
} | |
else{ | |
Write-Host "no value Width or Height." -ForegroundColor Yellow | |
throw | |
} | |
} | |
# 縮小先のオブジェクトを生成 | |
$canvas = New-Object System.Drawing.Bitmap([int]$w, [int]$h) | |
# 縮小先へ描画 | |
$graphics = [System.Drawing.Graphics]::FromImage($canvas) | |
$graphics.DrawImage($image, (New-Object System.Drawing.Rectangle(0, 0, $canvas.Width, $canvas.Height))) | |
# 回転 | |
$canvas.RotateFlip($Rotate) | |
# 拡張子 | |
# https://msdn.microsoft.com/ja-jp/library/system.drawing.imaging.imageformat(v=vs.110).aspx | |
$hash = @{".bmp"="Bmp"; ".jpg"="Jpeg"; ".jpeg"="Jpeg"; ".png"="Png" ;".gif"="Gif"} | |
$ImageFormat = $hash[$ext] | |
# 保存 | |
$canvas.Save("$Destination\$Name", [System.Drawing.Imaging.ImageFormat]::$ImageFormat) | |
Write-Verbose "Save $Name file from $($oldWidth)x$($oldHeight) to $($w)x$($h)." | |
# オブジェクトの破棄 | |
$graphics.Dispose() | |
$canvas.Dispose() | |
$image.Dispose() | |
# 上書き | |
if($Overwrite){ | |
$path = (Get-Item -Path $_).DirectoryName | |
Remove-Item -Path $_ -Force | |
Move-Item -Path "$Destination\$Name" -Destination $path -Force | |
Write-Verbose "Overwrite $Name." | |
} | |
} | |
} | |
End{} | |
} |