This is me, Wu!
» e-shell.org
In this page
Rate this page!
Search inside the wiki!
  Home >> hacks >> Doing some magic with images

Doing some magic with images

There is a package out there in the unix world, called ImageMagick?, that contains a nice tool, called convert, that could be used to perform some operations with images. For example, to resize an image creating what we commonly know as a thumbnail:

$ convert -size 50x50 image.jpg -resize 50x50 +profile "*" image_thumbnail.jpg

This will create a small copy of image.jpg scaled to 50x50 pixels, if the image do not scale perfectly to 50x50, it automatically resize it to a proper scale, like 50x40, 40x50 and so on, keeping the ratio of the image. Nice, isn't it? Well, applying some knowledge about shell scripting we can create thumbnails for all images in a given directory. Try that (asumming you have the bash shell installed on your system):

$ ls
images thumbs
$ cd images/
$ ls
imag001.jpg  imag002.jpg  imag003.jpg  imag004.jpg
$ for i in `ls .`
>  do
>   echo "Creating Thumbnail of image $i"
>   convert -size 50x50 $i -resize 50x50 +profile "*" ../thumbs/$i
>  done
Creating Thumbnail of image imag001.jpg
Creating Thumbnail of image imag002.jpg
Creating Thumbnail of image imag003.jpg
Creating Thumbnail of image imag004.jpg
$

This simple process could be indeed simplyfied in one line:

for i in `ls .; do; convert -size 50x50 $i -resize 50x50 +profile "*" ../thumbs/$i; done

Take care that this one will echo nothing on the terminal, so be patient while convert does its job. The convert util provides us with an easy an quick way to manipulate images, and it is specially interesting when we need to perform such manipulation from a automated process, a script, etc. Take a look at the man page to learn more about convert and ImageMagick?.