Oct 28, 2007

batch resizing images in linux

we often need to decrease the image size before uploading. You must be using some graphical software to resize them. Its not that tedious if its one or two but what if you have to resize a large numbers of images!!!. This is what i do.

Basically two commands are used here.
1. djpeg - decompress a JPEG file to an image file
2. cjpeg - compress an image file to a JPEG file

So to resize, i run this script on a folder containing the image files.


mkdir resized
for i in *.JPG
echo 'Resizing image..' $i
djpeg -scale 1/2 $i | cjpeg > resized/$i

After script is done, a folder named 'resized' is created containing the resized images.

Both djpeg and cjpeg aren't installed by default, they're part of libjpeg-progs package. Under Ubuntu you can get it by

sudo apt-get install libjpeg-progs

(source: jwalanta.blogspot.com)