-
-
Save voidproc/c9d8f7cb64022d4d18ca2951c071127f to your computer and use it in GitHub Desktop.
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
<# | |
.SYNOPSIS | |
画像ファイルを連結します。 | |
.DESCRIPTION | |
複数の画像ファイルを縦または横方向に連結し、保存します。 | |
保存される画像形式は、保存ファイル名の拡張子から判断されます。 | |
.PARAMETER Src | |
連結するファイル名をコンマ区切りまたはパイプラインから指定します。 | |
.PARAMETER Dst | |
出力するファイル名を指定します。このパラメータを省略した際の出力先は "out.png" になります。 | |
.PARAMETER Horizontal | |
画像を横方向に連結します。 | |
.EXAMPLE | |
> Save-CombinedImage -Src image1.png,image2.png -Dst new.png | |
.EXAMPLE | |
> Get-ChildItem -Path image*.png | Save-CombinedImage -Dst new.png -Horizontal | |
#> | |
function Save-CombinedImage { | |
param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[string[]] | |
$Src, | |
[string] | |
$Dst = "out.png", | |
[switch] | |
$Horizontal | |
) | |
begin | |
{ | |
$src_ = New-Object System.Collections.Generic.List[string] | |
} | |
process | |
{ | |
$Src | ForEach-Object { $src_.Add($_) } | |
} | |
end | |
{ | |
Add-Type -AssemblyName System.Drawing | |
# カレントディレクトリを $PWD に設定 | |
$pcd = [System.IO.Directory]::GetCurrentDirectory() | |
[System.IO.Directory]::SetCurrentDirectory($PWD) | |
# 指定された画像から Bitmap を作成 | |
$bmp = $src_ | ForEach-Object { New-Object System.Drawing.Bitmap($_) } | |
# 連結後のサイズ | |
$measureW = $bmp | ForEach-Object { $_.Width } | Measure-Object -Sum -Maximum | |
$measureH = $bmp | ForEach-Object { $_.Height } | Measure-Object -Sum -Maximum | |
[int] $w = $measureW.Maximum | |
[int] $h = $measureH.Sum | |
if ($Horizontal) | |
{ | |
$w = $measureW.Sum | |
$h = $measureH.Maximum | |
} | |
# 連結画像を作成 | |
$bmpCanvas = New-Object System.Drawing.Bitmap($w, $h) | |
$gfx = [System.Drawing.Graphics]::FromImage($bmpCanvas) | |
$gfx.FillRectangle([System.Drawing.Brushes]::Transparent, 0, 0, $w, $h) | |
$bmp | ForEach-Object { $x=0; $y=0 } { | |
$gfx.DrawImage($_, $x, $y); | |
if ($Horizontal) { $x += $_.Width } else { $y += $_.Height } | |
} | |
# 画像形式 | |
$format = switch -Regex ([System.IO.Path]::GetExtension($Dst)) | |
{ | |
"\.bmp" { "Bmp" } | |
"\.jpe?g" { "Jpeg" } | |
"\.gif" { "Gif" } | |
"\.png" { "Png" } | |
default { "Png" } | |
} | |
# 保存 | |
$bmpCanvas.Save($Dst, $format) | |
# 破棄 | |
$gfx.Dispose() | |
$bmpCanvas.Dispose() | |
$bmp | ForEach-Object { $_.Dispose() } | |
[System.IO.Directory]::SetCurrentDirectory($pcd) | |
} | |
} | |
<# | |
.SYNOPSIS | |
画像ファイルを拡大/縮小します。 | |
.DESCRIPTION | |
複数の画像ファイルを指定したサイズに拡大/縮小し、保存します。 | |
保存される画像形式は、保存ファイル名の拡張子から判断されます。 | |
.PARAMETER Src | |
連結するファイル名をコンマ区切りまたはパイプラインから指定します。 | |
.PARAMETER Width | |
拡大/縮小後の画像の横幅(px)を指定します。 | |
.PARAMETER Ratio | |
拡大/縮小率(%)を指定します。 | |
.PARAMETER Mode | |
拡大/縮小する際の補間方法を指定します。 | |
.EXAMPLE | |
> Get-ChildItem -Path image*.png | Save-ResizedImage -Ratio 0.5 -Mode High | |
#> | |
function Save-ResizedImage | |
{ | |
Param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[string[]] | |
$Src, | |
[int] | |
$Width = 0, | |
[double] | |
$Ratio = $null, | |
[ValidateSet("NearestNeighbor", "Bilinear", "Bicubic", "HighQualityBilinear", "HighQualityBicubic", "Low", "High", "Default")] | |
[string] | |
$Mode = "Default", | |
[switch] | |
$Silent | |
) | |
begin | |
{ | |
Add-Type -AssemblyName System.Drawing | |
$pcd = [System.IO.Directory]::GetCurrentDirectory() | |
[System.IO.Directory]::SetCurrentDirectory($PWD) | |
} | |
process | |
{ | |
if ($Width -eq 0 -and $Ratio -eq $null) | |
{ | |
return | |
} | |
$Src | Resolve-Path | ForEach-Object { | |
$src_path = $_.Path | |
if (!$Silent) { $_.Path } | |
$img_src = New-Object System.Drawing.Bitmap($src_path) | |
if ($Ratio) | |
{ | |
$Width = $img_src.Width * $Ratio | |
} | |
$w = $Width | |
$h = $img_src.Height * $Width / $img_src.Width | |
$img_dst = New-Object System.Drawing.Bitmap($w, $h) | |
$g = [System.Drawing.Graphics]::FromImage($img_dst) | |
$g.InterpolationMode = $Mode | |
$g.DrawImage($img_src, 0, 0, $w, $h) | |
$ext = [System.IO.Path]::GetExtension($src_path) | |
$format = switch -Regex ($ext) | |
{ | |
"\.bmp" { "Bmp" } | |
"\.jpe?g" { "Jpeg" } | |
"\.gif" { "Gif" } | |
"\.png" { "Png" } | |
default { "Png" } | |
} | |
$dst_path = [System.IO.Path]::ChangeExtension($src_path, ".resized$ext") | |
# 保存 | |
$img_dst.Save($dst_path, $format) | |
# 破棄 | |
$g.Dispose() | |
$img_dst.Dispose() | |
$img_src.Dispose() | |
} | |
} | |
end | |
{ | |
[System.IO.Directory]::SetCurrentDirectory($pcd) | |
} | |
} | |
Export-ModuleMember -Function Save-CombinedImage, Save-ResizedImage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment