{"id":10921,"date":"2006-04-25T12:17:48","date_gmt":"2006-04-25T12:17:48","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/image-conversion\/"},"modified":"2019-02-18T13:25:07","modified_gmt":"2019-02-18T20:25:07","slug":"image-conversion","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/image-conversion\/","title":{"rendered":"Image Conversion"},"content":{"rendered":"<div>&nbsp;<\/div>\n<div>I needed to convert some images from bmp to gif for a web site I&#8217;ve been working on.&nbsp; I figured that I could script this pretty easy &#8211; here&#8217;s what I came up with.&nbsp; It&#8217;s a little more general than I needed, and I thought it might be useful to others.&nbsp; Anyway, it&#8217;s useful to me.&nbsp; <\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;<\/div>\n<div><font face=\"Courier New\"># Convert one graphic image to another <br \/>param ( $inFile, $type = &#8220;gif&#8221;, $outFile, [switch]$force ) <br \/># <br \/># First check to see if our input file exists <br \/>$iFile = (resolve-path $inFile -ea silentlycontinue ).path <br \/>if ( ! $iFile ) { &#8220;File &#8216;$inFile&#8217; not found, exiting&#8221; ; exit } <\/font><\/div>\n<div><font face=\"Courier New\"><\/font>&nbsp;<\/div>\n<div><font face=\"Courier New\"># now check to see if the output file exists, if force <br \/># we will continue, otherwise we exit <br \/>if ( ! $outFile )<br \/>{<br \/>&nbsp;&nbsp;&nbsp; $tFile = get-item (resolve-path $inFile)<br \/>&nbsp;&nbsp;&nbsp; $outFile = $tFile.Fullname -replace ($tFile.Extension + &#8220;`$&#8221;),&#8221;.$type&#8221;<br \/>}<br \/>if ( (test-path $outFile) -and (! $force) ) { &#8220;File &#8216;$outFile&#8217; exists, exiting&#8221;; exit } <\/font><\/div>\n<div><font face=\"Courier New\"><\/font>&nbsp;<\/div>\n<div><font face=\"Courier New\"># make sure we have an encoder before changing anything on <br \/># the filesystem <br \/>$codecs = [drawing.imaging.ImageCodecInfo]::GetImageEncoders() | <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach { $h = @{} } { $h.($_.formatdescription) = $_ } { $h } <br \/>$encoder = $codecs.$type <br \/>if ( ! $encoder )&nbsp; <br \/>{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;No encoder of type &#8216;$type&#8217;, exiting&#8221;; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;Available encodings are: &#8221; + [string]::Join(&#8220;, &#8220;, $h.keys) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit <br \/>} <\/font><\/div>\n<div><font face=\"Courier New\"><\/font>&nbsp;<\/div>\n<div><font face=\"Courier New\"># This hoop is needed because resolve-path needs<br \/># the file to actually exist.&nbsp; We shouldn&#8217;t get here <br \/># unless the file doesn&#8217;t exist, or we&#8217;re going to remove it <br \/># by force. <br \/>if ( test-path $outFile ) { remove-item $outFile } <br \/>[void](new-item -type file $outFile) <br \/>$outFile = (resolve-path $outFile).path <br \/>remove-item $outFile <\/font><\/div>\n<div><font face=\"Courier New\"><\/font>&nbsp;<\/div>\n<div><font face=\"Courier New\"># read the image<br \/>$image = [system.drawing.image]::FromFile($iFile) <br \/>$image.Save($outFile, $encoder.FormatId)<br \/>$image.Dispose()<br \/># Get the file we just created <br \/>get-item $outFile<\/font> <\/div>\n<div>&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; I needed to convert some images from bmp to gif for a web site I&#8217;ve been working on.&nbsp; I figured that I could script this pretty easy &#8211; here&#8217;s what I came up with.&nbsp; It&#8217;s a little more general than I needed, and I thought it might be useful to others.&nbsp; Anyway, it&#8217;s useful [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; I needed to convert some images from bmp to gif for a web site I&#8217;ve been working on.&nbsp; I figured that I could script this pretty easy &#8211; here&#8217;s what I came up with.&nbsp; It&#8217;s a little more general than I needed, and I thought it might be useful to others.&nbsp; Anyway, it&#8217;s useful [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10921"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}