Partager via


Pretty as a Picture

I Told You So

In my last post I promised to try to do something more fun. For me that would mean some wicked algorithm, but I realize I’m in the minority on that one. I decided instead to pull out a script I did a while back to resize an image. I actually leveraged some .Net code I found on a blog which I can’t remember, so I acknowledge that some parts of this script I owe in large part to an unnamed donor. Many thanks to whoever you are. It wasn’t exactly what I was looking for, so the final product is mine—after a bit of effort.

Extraneous Background Information

When I wrote this script, I was working on a larger project where I had to create a boatload of very detailed performance metrics graphs for 100+ servers and make them available from a SharePoint site. Each graph would be displayed on its own page in full resolution, but it also needed to be displayed in lower resolution on a summary page. The images had to be clear enough that a user could spot trends on individual servers and make a decision about whether or not to look at the individual graph for further investigation. It quickly became clear that I needed to generate thumbnail versions of all of my images. So that’s the challenge that motivated me to write this script.

The Script

Param( [System.IO.FileInfo[]] $InputFile ,

                              $Suffix = "_Resized" ,

                              $NewWidth = 225 ,

                              $NewHeight = 150 )

If (-not $InputFile) { $ScriptInputFile = $Input }

Else {$ScriptInputFile = $InputFile}

[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

$ScriptInputFile | %{

    $FileImage = ([System.Drawing.Image]::FromFile("$($_.FullName)"))

    $Bitmap = New-Object -TypeName System.Drawing.Bitmap `

    -ArgumentList $NewWidth,$NewHeight,$FileImage.PixelFormat

    $Graphic = [System.Drawing.Graphics]::FromImage($Bitmap)

    $Graphic.SmoothingMode = `

    [System.Drawing.Drawing2D.SmoothingMode]::HighQuality

    $Graphic.InterpolationMode = `

    [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic

    $Graphic.DrawImage($FileImage, 0, 0, $Bitmap.Width, $Bitmap.Height)

    $FileType = $_.FullName.Split(".")[-1]

    $Bitmap.Save($_.FullName.Replace(".$FileType","$Suffix.$FileType"), `

    [System.Drawing.Imaging.ImageFormat]::Jpeg)

    $Graphic.Dispose()

}

 

Parameters

-InputFile <Image File or files>

You can run the script specifying a single file or a collection of FileInfo objects. If you specify a string, it will attempt to convert the string to a valid FileInfo object.

-Suffix <Suffix string>

This gets appended to your newly resized file name. Default is “_Resized.”

-NewWidth <New width>

Specify the new width in pixels. Default is 225.

-NewHeight <New height>

Specify the new width in pixels. Default is 150.

 

Seeing it in Action

Given the following sample image (object in browser are larger than the appear—the original size of this image is 1024x683):

Tulip

The first line here will resize the file and return the FileInfo object. The second command will actually display the shrunken file in the default application.

 

[PS] > Set-Image.ps1 -InputFile "C:\Users\Public\Pictures\Sample Pictures\tulip.jpg" -Suffix ".Tiny" -NewWidth 25 -NewHeight 16

    Directory: C:\Users\Public\Pictures\Sample Pictures

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a--- 6/2/2009 12:35 AM 817 tulip.Tiny.jpg

[PS] > & (Set-Image.ps1 -InputFile "C:\Users\Public\Pictures\Sample Pictures\tulip.jpg" -Suffix ".Tiny" -NewWidth 25 -NewHeight 16)

tulip.Tiny

Challenge!

Note that it will match your NewWidth and NewHeight parameters and skew the image if necessary. I can imagine it wouldn’t be that difficult to modify the script to prevent skewing or to specify a percentage to use, so the new image would be 75% or 200% of the original. The System.Drawing.Image object has Height and Width properties that would give you the dimensions of the original image. Hint, hint!