LendKey

Wednesday, November 28, 2007

Get default folders in Windows

In order to get any system folder under Windows (I guess from 95 to Vista) by any programming language, you can call function:

HRESULT SHGetFolderPath( HWND hwndOwner,
int nFolder,
HANDLE hToken,
DWORD dwFlags,
LPTSTR pszPath
);
( http://msdn2.microsoft.com/en-us/library/ms647764.aspx )

and pass in the folder you are looking for, such as:
CSIDL_MYDOCUMENTS: My Document
CSIDL_COMMON_APPDATA: C:\Documents and Settings\All Users\Application Data
CSIDL_PROGRAM_FILES: C:\Program Files.

Check the following link for all IDs:
http://msdn2.microsoft.com/en-us/library/bb762494.aspx

Hope these two links can be handy to you.

Friday, November 23, 2007

Some tricks on ImageMagick

I had about 1000 images with all different resolutions. I needed to resize them to fit into 100x100 square. So I looked around and found a great command line tool that can do lots of different image manipulation in a batch.

The tools is called Image Magick, you can find it at:
http://www.imagemagick.org/script/index.php

After downloaded a Windows version and installed, I had a brief look at the manual find out the way to resize everything into a specific size and keep the image ratio at the same time:

c:convert -resize 100x100 -quality 100 ..\*.* %d.jpg

Basically I create a new folder under the folder where all my original images are located. Went to he new folder and ran the command above.

The command resized all image files under the parent folder ( original files ) to 100x100 area and kept original image ratio. The result files are named as numbers from 0.jpg to 999.jpg. All original files were converted to JPG format,not matter what they were.

Please be advised, the result image might not be exact 100x100. For example, if the original image is 400x200, the new image will end up as 100x50. You got the idea?

I set quality to 100, so shrunk image still looks pretty nice.

You can also add one more option -strip which is very helpful for me to strip out all image information stored in each file, such as tags, comments, etc. and saved me lots of space.

So far so good, untilthe second day. I suddenly realized an issue, when I show the images on my website in a 100x100 area, my tag on site requires 100x100 exact size, otherwise, the image will be stretched and got disordered. So I need to do another conversion:

convert -gravity Center -extent 100x100 ..\*.jpg %d.jpg

By this command, I was able to convert everything int a new image with exact 100x100 resolution. By using -gravity Center, the original image got placed at center of the new image. So everything is working perfectly now!

There're hundreds of commands provided by Image Magick. You have to either spend hours reading through all of them and become a guru, or like I did, simply search a word I want to work on my images, and jump to that command directly and try.

Image Magick is surely a great tool. I hope I can find more real-life tricks playing with it in the future. And hope you enjoy playing with it yourself too.

-Li