Hey, Scripting Guy! How Can I Use Windows PowerShell to Convert Graphics Files to Different File Formats?

ScriptingGuy1

  Hey, Scripting Guy! Question Hey, Scripting Guy! I have a number of graphics files that I need to convert to a different file format. Can I use Windows PowerShell 2.0 to convert a .bmp file to a .jpg file? — SW   Hey, Scripting Guy! Answer Hello SW, Microsoft Scripting Guy Ed Wilson here. It is Guest Blogger Week here on the Script Center. Today, we have William Stanek talking about working with graphic files. Take it away William…   I finished up work on Windows PowerShell 2.0 Administrator’s Pocket Consultant awhile back. Recently, the Scripting Guys asked me if I could write some guest columns, so I began looking at what I’d written already and what I could write about. For starters, how about image conversion and manipulation using System.Drawing.Bitmap? Using System.Drawing.Bitmap, you can convert to or from BMP, GIF, JPEG, PNG, TIFF, and WMF formats. The following example loads the required .NET Framework assemblies, gets an object reference for the image, and then displays the height and width of the image. The variable $i holds the image reference:  

[Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”);
$i = new-object System.Drawing.Bitmap test.tiff;
$i.height;
$i.width;

Here, the test image is in your working directory and called test.tiff. You can substitute whatever you’ve named your image (or reference a file path). Now that you have an object reference, you can work with the image. You could rotate the image 90 degrees:  

$i.rotateflip(“Rotate90FlipNone”);

Alternatively, rotate 180 degrees using Rotate180FlipNone or rotate 270 degrees using Rotate270FlipNone. Also, you can FlipX, FlipY, or FlipXY instead of FlipNone. The complete set of options is as follows:

Name

Description

RotateNoneFlipNone

Uses no rotation and no flipping.

Rotate90FlipNone

Uses a 90-degree rotation without flipping.

Rotate180FlipNone

Uses a 180-degree rotation without flipping.

Rotate270FlipNone

Uses a 270-degree rotation without flipping.

RotateNoneFlipX

Uses no rotation followed by a horizontal flip.

Rotate90FlipX

Uses a 90-degree rotation followed by a horizontal flip.

Rotate180FlipX

Uses a 180-degree rotation followed by a horizontal flip.

Rotate270FlipX

Uses a 270-degree rotation followed by a horizontal flip.

RotateNoneFlipY

Uses no rotation followed by a vertical flip.

Rotate90FlipY

Uses a 90-degree rotation followed by a vertical flip.

Rotate180FlipY

Uses a 180-degree rotation followed by a vertical flip.

Rotate270FlipY

Uses a 270-degree rotation followed by a vertical flip.

RotateNoneFlipXY

Uses no rotation followed by a horizontal and vertical flip.

Rotate90FlipXY

Uses a 90-degree rotation followed by a horizontal and vertical flip.

Rotate180FlipXY

Uses a 180-degree rotation followed by a horizontal and vertical flip.

Rotate270FlipXY

Uses a 270-degree rotation followed by a horizontal and vertical flip.

The working copy of the image is not saved yet to the file system. You can save the modified image in BMP, GIF, JPEG, PNG, TIFF, or WMF format. Keep in mind that you may lose quality going from a format such as TIFF to a format such as JPEG. The following example saves in GIF format:  

$i.Save(“test.gif”,“GIF”);

To display the properties of the image object you are working with, use:  

$i;

To display the complete set of methods and properties for System.Drawing.Bitmap objects, use:  

$i | get-member;

The complete script is shown here. ConvertAndFlipGraphicFile.ps1 #Load required assemblies and get object reference
[Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”);
$i = new-object System.Drawing.Bitmap(“C:fsoscript.jpg”);
#Display image properties including height and width
$i;
#Play with the image
$i.rotateflip(“Rotate90FlipNone”);
#Save with the image in the desired format
$i.Save(“c:fsoscript.gif”,“GIF”); The script.jpg image is shown here. The script.jpg image After running the script, the figure is flipped and the picture format is changed. A .gif file is associated with Internet Explorer by default. The new image is shown here. The image rotated and in .gif format Although image manipulation isn’t really an administrative feature discussed in the Windows PowerShell 2.0 Administrator’s Pocket Consultant, the .NET Framework is. Hopefully, this is fun for you to play with and helps you get started with the .NET Framework and objects. Thanks for reading! It’s time for me to get back to work. I hope you’ll take a look at my new book, Windows 7: The Definitive Guide. Also, my book, Microsoft Exchange Server 2010 Administrator’s Pocket Consultant, was recently released. Make some noise and I’ll gladly blog about Windows PowerShell for Exchange and maybe even for SQL Server. –William R. Stanek (williamstanek at aol dot com)–   SW, that is all there is to using Windows PowerShell to work with images. Guest Blogger Week will continue tomorrow. If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.  

Ed Wilson and Craig Liebendorfer, Scripting Guys

0 comments

Discussion is closed.

Feedback usabilla icon