Compress-Bitmap (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1290"></script>download | new post
Compress a bitmap to a certain filesize, using the PSCX Import-Bitmap, Resize-Bitmap, and Export-Bitmap … and a little trial and error.
- function Compress-Bitmap {
- PARAM(
- [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
- [IO.FileInfo]$SourceFile
- ,
- [Parameter(Mandatory=$true, Position=1)]
- [String]$DestinationFile
- ,
- [Parameter(Mandatory=$false)]
- [Int]$Width
- , [Parameter(Mandatory=$false)]
- [Int]$Height
- , [Parameter(Mandatory=$false)]
- [Int]$MaxFilesize
- , [Parameter(Mandatory=$false)]
- [Int]$Quality = 100
- )
- BEGIN { if($SourceFile) { $SourceFile = Get-ChildItem $SourceFile } }
- PROCESS {
- # Work our way down until we get a small enough file (this might be slow)
- [string]$intermediate = [IO.path]::GetRandomFileName() + ".jpeg"
- $bitmap = Import-Bitmap $SourceFile
- if($Width -and $Height) {
- $bitmap = Resize-Bitmap -Bitmap $bitmap -Width $Width -Height $Height
- } else { # work around another bug in Export-Bitmap
- $bitmap = Resize-Bitmap -Bitmap $bitmap -Percent 100
- }
- do {
- Export-Bitmap -Bitmap $bitmap -Path $intermediate -Quality ($Quality--)
- } while( $MaxFilesize -and ((Get-ChildItem $intermediate).Length -gt $MaxFilesize))
- Write-Host "Output Quality: $($Quality + 1)%" -Foreground Yellow
- Move-Item $intermediate $DestinationFile -Force -Passthru
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository