Hey, Scripting Guy! How Can I Use Windows PowerShell to Convert Graphics Files to Different File Formats?
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?
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:
$i = new-object System.Drawing.Bitmap test.tiff;
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:
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:
Uses no rotation and no flipping.
Uses a 90-degree rotation without flipping.
Uses a 180-degree rotation without flipping.
Uses a 270-degree rotation without flipping.
Uses no rotation followed by a horizontal flip.
Uses a 90-degree rotation followed by a horizontal flip.
Uses a 180-degree rotation followed by a horizontal flip.
Uses a 270-degree rotation followed by a horizontal flip.
Uses no rotation followed by a vertical flip.
Uses a 90-degree rotation followed by a vertical flip.
Uses a 180-degree rotation followed by a vertical flip.
Uses a 270-degree rotation followed by a vertical flip.
Uses no rotation followed by a horizontal and vertical flip.
Uses a 90-degree rotation followed by a horizontal and vertical flip.
Uses a 180-degree rotation followed by a horizontal and vertical flip.
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:
To display the properties of the image object you are working with, use:
To display the complete set of methods and properties for System.Drawing.Bitmap objects, use:
The complete script is shown here.
#Load required assemblies and get object reference
$i = new-object System.Drawing.Bitmap(“C:fsoscript.jpg”);
#Display image properties including height and width
#Play with the image
#Save with the image in the desired format
The script.jpg image is shown here.
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.
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 email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys