{"id":4281,"date":"2009-03-30T19:52:00","date_gmt":"2009-03-30T19:52:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/03\/30\/image-manipulation-in-powershell\/"},"modified":"2019-02-18T13:12:44","modified_gmt":"2019-02-18T20:12:44","slug":"image-manipulation-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/image-manipulation-in-powershell\/","title":{"rendered":"Image Manipulation in PowerShell"},"content":{"rendered":"<p>The other week, I showed a Get-ProgID function that I used to help someone at Microsoft Research find a good object to talk to images.&nbsp; A few comments jumped to one of the possible solutions, which is to use the .NET Drawing assembly (which I have to load first).&nbsp; I wanted to be reasonably sure that the way to get at the image metadata was efficient and didn\u2019t require loading up any assemblies PowerShell hadn\u2019t loaded already.&nbsp; Looking at the output of Get-ProgID, Two COM objects jumped out at me:<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms630506(VS.85).aspx\">Wia.ImageFile<\/a> <\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms630507(VS.85).aspx\">Wia.ImageProcess<\/a> <\/li>\n<\/ul>\n<p>These two COM objects are part of a whole fun family of scriptable COM objects that work with the Microsoft Windows Image Acquisition Layer.&nbsp; WIA has been in Windows since XP SP1, and it doesn\u2019t just give me a cheap way to get at the width and height of my image, it gives me a way to crop, resize, overlay, and rotate my images too.&nbsp; <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms630826(VS.85).aspx#SharedSample001\">Check out this page<\/a> for a ton of examples of fun things you can do with WIA.<\/p>\n<p>I gave the Microsoft Research friend the solution to the width and height problem last week.&nbsp; Supposing I had a file, try.jpg, in my current directory, here\u2019s how I\u2019d get at the width and the height of that file:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span>$image<\/span> <span>=<\/span> <span>New-Object<\/span> <span>-ComObject<\/span> <span>Wia.ImageFile<\/span>\n<span>$image<\/span><span>.<\/span><span>LoadFile<\/span><span>(<\/span><span>\"$pwd\\try.jpg\"<\/span><span>)<\/span>\n<span>$image<\/span><span>.<\/span><span>Width<\/span><span>,<\/span> <span>$image<\/span><span>.<\/span><span>Height<\/span><\/pre>\n<p>I think this is pretty straightforward way to get at the&nbsp; image properties without having to load up new assemblies.&nbsp; Since I figured out that more cool scenarios were possible with the these COM objects, I couldn\u2019t really resist writing a more complete image manipulation module in PowerShell.<\/p>\n<p>So far, this library includes functions that help resize, crop, overlay, and rotate images.&nbsp; It also contains a function, Get-Image, which will take the output of Get-ChildItem and return all files WIA can load with nifty little script methods to crop and scale the current image. Each function has inline help and examples.<\/p>\n<p>The module is attached to the post in a .ZIP file.&nbsp; Simply download and unzip the file into a directory called Image underneath $env:UserProfile\\Documents\\WindowsPowerShell\\Modules and then run Import-Module Image. I hope you have as much fun with it as have.<\/p>\n<p>Hope this helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/09\/52\/07\/77\/Image.zip\">Image.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other week, I showed a Get-ProgID function that I used to help someone at Microsoft Research find a good object to talk to images.&nbsp; A few comments jumped to one of the possible solutions, which is to use the .NET Drawing assembly (which I have to load first).&nbsp; I wanted to be reasonably sure [&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":[97,137],"class_list":["post-4281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-advanced-functions","tag-ctp3"],"acf":[],"blog_post_summary":"<p>The other week, I showed a Get-ProgID function that I used to help someone at Microsoft Research find a good object to talk to images.&nbsp; A few comments jumped to one of the possible solutions, which is to use the .NET Drawing assembly (which I have to load first).&nbsp; I wanted to be reasonably sure [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4281","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=4281"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4281\/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=4281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}