LendKey

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

No comments: