Saturday, October 11, 2008

I was working with a lot of images for a website today and I needed to quickly create thumbnails for each one. I did a quick Google search on PowerShell and creating thumbnails, but I gave up after about a minute… yeah, I probably got impatient, but oh well.

Anyway, here it is:

get-childitem *.jpg | foreach {
    $full = [System.Drawing.Image]::FromFile("$(resolve-path $_)");
    $thumb = $full.GetThumbnailImage(72, 72, $null, [intptr]::Zero);
    $thumb.Save("$(resolve-path $_).thumb.jpg" );
    $full.Dispose();
    $thumb.Dispose();
}

Obviously, you likely wouldn’t hardcode the height and width for the thumbnail, but would probably base it off of the original image (i.e. varying it off of whether the image was wide or tall). But, I didn’t need to. That’s up to you to figure out :-)

Wednesday, November 05, 2008 9:43:42 PM (Central Standard Time, UTC-06:00)
Good one! Bookmarked for an upcoming powerscripting podcast.

-hal
Thursday, November 06, 2008 8:02:33 AM (Central Standard Time, UTC-06:00)
Very cool :)
Friday, November 21, 2008 8:06:29 AM (Central Standard Time, UTC-06:00)
Hi David,
There is one issue with that. If you use just Save(), the default output format is PNG file and some programs then reports that the file has bad extension (IrfanView for example). So there are two solutions:
1) Use different file extension on line 4
2) Use different Save() method with ImageFormat defined

For me option one is working.
Thank you very much for the script, I like it :)
David
Saturday, November 22, 2008 9:13:18 AM (Central Standard Time, UTC-06:00)
Hi David, thanks for the comment!

Yeah, defining the ImageFormat would make the script more descriptive about its intentions. I pulled up Reflector out of curiosity to see what the Save method did by default and it looks like it sets the calls the Save method with a value of RawFormat. I'm not sure if that looks at the value of the image prior to making it a thumbnail or what. Either way, good to know!
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, i, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview