Last active
August 29, 2015 14:08
-
-
Save yacchin1205/925851027b84240b512f to your computer and use it in GitHub Desktop.
PowerShell Scripts
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
param( | |
[Parameter(Mandatory=$True)] | |
[string]$out, | |
[Parameter()] | |
[int]$margin = 16, | |
[Parameter()] | |
[int]$margintop = -1, | |
[Parameter()] | |
[int]$marginbottom = -1, | |
[Parameter()] | |
[int]$marginleft = -1, | |
[Parameter()] | |
[int]$marginright = -1, | |
[Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[Alias('FullName')] | |
[String[]]$filePaths | |
) | |
begin { | |
$ErrorActionPreference = "Stop" | |
[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing') | |
$outPath = Resolve-Path $out | |
$pmargintop = $margintop | |
if($pmargintop -lt 0) { | |
$pmargintop = $margin | |
} | |
$pmarginbottom = $marginbottom | |
if($pmarginbottom -lt 0) { | |
$pmarginbottom = $margin | |
} | |
$pmarginleft = $marginleft | |
if($pmarginleft -lt 0) { | |
$pmarginleft = $margin | |
} | |
$pmarginright = $marginright | |
if($pmarginright -lt 0) { | |
$pmarginright = $margin | |
} | |
} | |
process { | |
foreach($filePath in $filePaths) { | |
$bitmap = New-Object Drawing.Bitmap ((Resolve-Path $filePath).ToString()) | |
if($null -eq $bitmap) { | |
continue | |
} | |
try { | |
$width = $bitmap.Width + $pmarginleft + $pmarginright | |
$height = $bitmap.Height + $pmargintop + $pmarginbottom | |
$resized = New-Object Drawing.Bitmap $width, $height, $bitmap.PixelFormat | |
try { | |
$g = [Drawing.Graphics]::FromImage($resized) | |
try{ | |
$g.FillRectangle([Drawing.Brushes]::White, (New-Object Drawing.Rectangle 0, 0, $width, $height)) | |
$g.DrawImage($bitmap, (New-Object Drawing.Rectangle $pmarginleft, $pmargintop, $bitmap.Width, $bitmap.Height)) | |
}finally{ | |
$g.Dispose() | |
} | |
$destpath = Join-Path $outPath ([IO.Path]::GetFileNameWithoutExtension($filePath) + "-margin.png") | |
$resized.Save($destpath, [Drawing.Imaging.ImageFormat]::Png) | |
Get-Item $destpath | |
} finally { | |
$resized.Dispose() | |
} | |
}finally{ | |
$bitmap.Dispose() | |
} | |
} | |
} |
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
param( | |
[Parameter(Mandatory=$True)] | |
[string]$out, | |
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] | |
[Alias('FullName')] | |
[String[]]$filePaths | |
) | |
begin { | |
$ErrorActionPreference = "Stop" | |
Add-Type -AssemblyName Microsoft.Office.InterOp.PowerPoint | |
$outPath = Resolve-Path $out | |
} | |
process { | |
$ppApp = New-Object -com PowerPoint.Application | |
try{ | |
foreach($filePath in $filePaths) { | |
$pptPres = $ppApp.Presentations.Open((Resolve-Path $filePath)) | |
$fileName = [IO.Path]::GetFileNameWithoutExtension((Split-Path -Leaf $filePath)) | |
$tempDirectory = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid()) | |
try{ | |
$pptPres.SaveAs($tempDirectory, [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::PpSaveAsPNG) | |
foreach($tempFile in Get-ChildItem $tempDirectory) { | |
$destImageName = $fileName + "-" + (Split-Path -Leaf $tempFile.FullName) | |
Move-Item -Force -PassThru $tempFile.FullName (Join-Path $outPath ($fileName + "-" + (Split-Path -Leaf $tempFile.FullName))) | |
} | |
Remove-Item $tempDirectory | |
}finally{ | |
$pptPres.Close() | |
} | |
} | |
}finally{ | |
$ppApp.Quit() | |
[gc]::collect() | |
[gc]::WaitForPendingFinalizers() | |
[gc]::collect() | |
} | |
} |
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
param( | |
[Parameter(Mandatory=$True)] | |
[string]$out, | |
[Parameter()] | |
[int]$width = -1, | |
[Parameter()] | |
[int]$height = -1, | |
[Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[Alias('FullName')] | |
[String[]]$filePaths | |
) | |
begin { | |
$ErrorActionPreference = "Stop" | |
[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing') | |
$outPath = Resolve-Path $out | |
} | |
process { | |
foreach($filePath in $filePaths) { | |
$bitmap = New-Object Drawing.Bitmap ((Resolve-Path $filePath).ToString()) | |
if($null -eq $bitmap) { | |
continue | |
} | |
try { | |
$xscale = 1.0 | |
if(($bitmap.Width -gt $width) -and ($width -gt 0)) { | |
$xscale = $width / $bitmap.Width | |
} | |
$yscale = 1.0 | |
if(($bitmap.Height -gt $height) -and ($height -gt 0)) { | |
$yscale = $height / $bitmap.Height | |
} | |
$scale = [Math]::Min($xscale, $yscale) | |
$rwidth = ($bitmap.Width * $scale) -as [int] | |
$rheight = ($bitmap.Height * $scale) -as [int] | |
$resized = New-Object Drawing.Bitmap $bitmap, $rwidth, $rheight | |
try { | |
$destpath = Join-Path $outPath ([IO.Path]::GetFileNameWithoutExtension($filePath) + "-resize.png") | |
$resized.Save($destpath, [Drawing.Imaging.ImageFormat]::Png) | |
Get-Item $destpath | |
} finally { | |
$resized.Dispose() | |
} | |
}finally{ | |
$bitmap.Dispose() | |
} | |
} | |
} |
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
param( | |
[Parameter(Mandatory=$True)] | |
[string]$out, | |
[Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[Alias('FullName')] | |
[String[]]$filePaths | |
) | |
begin { | |
$ErrorActionPreference = "Stop" | |
[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing') | |
$outPath = Resolve-Path $out | |
function findxedge($bitmap, $range1, $range2) { | |
foreach($i in $range1) { | |
$notblank = 0 | |
foreach($j in $range2) { | |
if($bitmap.GetPixel($i, $j).ToArgb() -ne 0xffffffff) { | |
$notblank ++ | |
break | |
} | |
} | |
if($notblank -ne 0) { | |
return $i | |
} | |
} | |
return -1 | |
} | |
function findyedge($bitmap, $range1, $range2) { | |
foreach($i in $range1) { | |
$notblank = 0 | |
foreach($j in $range2) { | |
if($bitmap.GetPixel($j, $i).ToArgb() -ne 0xffffffff) { | |
$notblank ++ | |
break | |
} | |
} | |
if($notblank -ne 0) { | |
return $i | |
} | |
} | |
return -1 | |
} | |
} | |
process { | |
foreach($filePath in $filePaths) { | |
$bitmap = New-Object Drawing.Bitmap ((Resolve-Path $filePath).ToString()) | |
if($null -eq $bitmap) { | |
continue | |
} | |
try { | |
$x1 = findxedge $bitmap (0..($bitmap.Width-1)) (0..($bitmap.Height-1)) | |
if($x1 -lt 0) { | |
continue | |
} | |
$x2 = findxedge $bitmap (($bitmap.Width-1)..$x1) (0..($bitmap.Height-1)) | |
$y1 = findyedge $bitmap (0..($bitmap.Height-1)) ($x1..$x2) | |
$y2 = findyedge $bitmap (($bitmap.Height-1)..$y1) ($x1..$x2) | |
$resized = $bitmap.Clone((New-Object Drawing.Rectangle $x1, $y1, ($x2-$x1+1), ($y2-$y1+1)), $bitmap.PixelFormat) | |
try { | |
$destpath = Join-Path $outPath ([IO.Path]::GetFileNameWithoutExtension($filePath) + "-trim.png") | |
$resized.Save($destpath, [Drawing.Imaging.ImageFormat]::Png) | |
Get-Item $destpath | |
} finally { | |
$resized.Dispose() | |
} | |
}finally{ | |
$bitmap.Dispose() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment