{"id":50153,"date":"2010-05-01T00:01:00","date_gmt":"2010-05-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/05\/01\/hey-scripting-guy-weekend-scripter-converting-png-files-to-jpeg-format\/"},"modified":"2010-05-01T00:01:00","modified_gmt":"2010-05-01T00:01:00","slug":"hey-scripting-guy-weekend-scripter-converting-png-files-to-jpeg-format","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-converting-png-files-to-jpeg-format\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Converting PNG Files to JPEG Format"},"content":{"rendered":"<p><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<p class=\"Readeraidonly\"><font face=\"Segoe UI\">This week saw the kickoff and first week of the <\/font><a href=\"http:\/\/www.bit.ly\/2010sgall\"><font face=\"Segoe UI\">2010 Scripting Games<\/font><\/a><font face=\"Segoe UI\">. The Scripting Wife is in the midst of things, so we decided to give her a break and let her catch her breath. You can catch up with her progress by reviewing the <\/font><a href=\"http:\/\/bit.ly\/scriptingwife\"><font face=\"Segoe\">articles in the archive<\/font><\/a><font face=\"Segoe UI\">. <\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">Microsoft Scripting Guy Ed Wilson here. This week has been absolutely crazy. The first week of the 2010 Scripting Games are drawing to a close, and I have been busy grading hundreds of scripts that have been submitted. You can review those scripts, and pick up some awesome scripting techniques by popping over to <\/font><a href=\"http:\/\/2010sg.poshcode.org\/\"><span><font face=\"Segoe UI\">PoshCode<\/font><\/span><\/a><font face=\"Segoe UI\">. You do not need to have a profile or be logged in to browse the scripts. Luckily, the scripting community has pitched in to aid in the grading, and we have more than a dozen judges. <\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">When I was collecting the articles written by the expert commentators, many of the writers submitted their articles with images in .png format because that is the default setting for the Snipping Tool that comes with Windows Vista and Windows 7. The Snipping Tool is really cool because it allows you to take pictures of your desktop, a program window, or even a freeform snip. The .png format is really nice because it is a relatively compact format that provides good resolution. However, we use .jpg pictures in our blog. <\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">Many of the commentators provided several pictures for their articles, and therefore I quickly ended up with a lot of pictures that I needed to convert to .jpg. Initially, I was copying the picture into Paint and using Save As to make the conversion because Paint in Windows 7 has been upgraded to make format conversions, but that quickly became a pain. I figured I could write a script to convert the file formats, but I had never seen such a script, and I was not even certain it could be done. So I decided to spend a little time on MSDN to see if there was a .NET Framework class I could use in Windows PowerShell 2.0. The ConvertPngToJpg.ps1 script is the result. <\/font><\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>ConvertPngToJpg.ps1<\/strong><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">#Requires \u2013Version 2.0<br \/>$sourceFile = &#8220;C:\\fso\\capture.png&#8221;<br \/>$saveFile = &#8220;C:\\fso\\capture.jpg&#8221;<br \/>Add-Type -AssemblyName system.drawing<br \/>$imageFormat = &#8220;System.Drawing.Imaging.ImageFormat&#8221; -as [type]<br \/>$image = [drawing.image]::FromFile($sourceFile)<br \/>$image.Save($saveFile, $imageFormat::jpeg)<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">The first thing I do in the ConvertPngToJpg.ps1 script is specify the path to the source file and the path for the converted file:<\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">$sourceFile = &#8220;C:\\fso\\capture.png&#8221;<br \/>$saveFile = &#8220;C:\\fso\\capture.jpg&#8221;<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">The next thing I do is load the <b>system.drawing<\/b> .NET Framework assembly. This will provide access to the <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.drawing.image.aspx\"><span><font face=\"Segoe\">drawing.image<\/font><\/span><\/a><font face=\"Segoe UI\"> .NET Framework class. In Windows PowerShell 2.0, the <b>Add-Type<\/b> cmdlet is used to load .NET Framework assemblies. In Windows PowerShell 1.0, you had to use Reflection, which was more complicated. When loading a .NET Framework assembly to provide access to a class, MSDN can be used to tell you which assembly contains the class you are interested in obtaining. This is important because at times the name of the assembly does not match the namespace that contains the class (in our example, the two do in fact match). <\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">MSDN tells us that the <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/9t4syfhh(v=VS.100).aspx\"><span><font face=\"Segoe\">save method<\/font><\/span><\/a><font face=\"Segoe UI\"> from the <b>image<\/b> class uses a <b>type<\/b> enumeration value to tell it what image type to save. To work with this enumeration directly, I needed to create an instance of the <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.drawing.imaging.imageformat_properties(v=VS.100).aspx\"><span><font face=\"Segoe\">ImageFormat type<\/font><\/span><\/a><font face=\"Segoe UI\">. Nearly a dozen different image formats are listed in this enumeration.<span>&nbsp; <\/span>The code that creates the <b>ImageFormat<\/b> type is seen here:<\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">$imageFormat = &#8220;System.Drawing.Imaging.ImageFormat&#8221; -as [type]<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">To load the image that is to be converted, use the <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/stf701f5(v=VS.100).aspx\"><span><font face=\"Segoe\">FromFile static method<\/font><\/span><\/a><font face=\"Segoe UI\">. The <b>FromFile<\/b> method accepts a single path to the file that will converted as shown here, but it can also accept a <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/4sahykhd(v=VS.100).aspx\"><span><font face=\"Segoe\">Boolean value that controls color management information<\/font><\/span><\/a><font face=\"Segoe UI\">:<\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">$image = [drawing.image]::FromFile($sourceFile)<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">After the image is loaded, we save it while specifying the image format. There are <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/84767bxk(v=VS.100).aspx\"><span><font face=\"Segoe\">other options that can be specified when saving the image<\/font><\/span><\/a><font face=\"Segoe UI\">, and they are detailed on MSDN. The code to save the image is shown here:<\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">$image.Save($saveFile, $imageFormat::jpeg)<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">When the script runs, it creates a .jpg file from the .png file. The two images are seen in the folder shown in the following image.<\/font><\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of .jpg file and .png file\" alt=\"Image of .jpg file and .png file\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/telligent.evolution.components.attachments\/13\/7619\/00\/00\/03\/32\/95\/77\/WES-5-1-10-1.JPG\" width=\"600\" height=\"321\"><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/telligent.evolution.components.attachments\/13\/7619\/00\/00\/03\/32\/95\/77\/WES-5-1-10-1.JPG\"><font face=\"Segoe\"><\/font><\/a><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">&nbsp;<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">If you want to know exactly what we will be looking at tomorrow, follow us on <\/font><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe UI\">Twitter<\/font><\/a><font face=\"Segoe UI\"> or <\/font><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><font face=\"Segoe UI\">Facebook<\/font><\/a><font face=\"Segoe UI\">. If you have any questions, send e-mail to us at <\/font><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a><font face=\"Segoe UI\"> or post your questions on the <\/font><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a><font face=\"Segoe UI\">. See you tomorrow. Until then, peace.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Segoe UI\">&nbsp;<\/font><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; This week saw the kickoff and first week of the 2010 Scripting Games. The Scripting Wife is in the midst of things, so we decided to give her a break and let her catch her breath. You can catch up with her progress by reviewing the articles in the archive. Microsoft Scripting Guy Ed [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[122,123,3,61,45],"class_list":["post-50153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphics","tag-multimedia","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; This week saw the kickoff and first week of the 2010 Scripting Games. The Scripting Wife is in the midst of things, so we decided to give her a break and let her catch her breath. You can catch up with her progress by reviewing the articles in the archive. Microsoft Scripting Guy Ed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=50153"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50153\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=50153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}