Wonders of ImageMagick

Monday, January 18, 2010
ImageMagick is a wonderful tool to manipulate images. I have not used it much before.
I had few pictures from a colleague's wedding party. These were the originals taken with with digital camera. And the size of the images was huge., 71 photos taking up 150MB disk space. So, today I wanted to reduce the storage size by keeping the quality of the images same. Also, the resolution of the photos were high; e.g. 2592x1944 pixels and I don't need such high resolution for there photos.
So, I started searching on Google and found an article describing convert program from ImageMagick. I tried convert to test on few photos as prescribed
$ convert -sample 50%x50% [source photo] [output photo]
note there I used percentage, have not gone for a fixed size as different photos had different resolutions, most of them are in landscape but few are in portrait shape.
50% size worked good, quality of the images are good and size has been reduced to one-third.
Tried 40%, size became nearly one-fifth but pictures started showing differences and I was not happy with them.
Then I started to read about "sample", and found about "resample", "scale", "resize" and "adaptive-resize". Tried resize, adapative-resize and scale with 40% and voilĂ , scale showed the most acceptable quality with best size of the photo (one-fifth of original).
$ convert -scale 40%x40% DSC06437.JPG scale40.jpg
$ convert -resize 40%x40% DSC06437.JPG resize40.jpg
$ convert -adaptive-resize 40%x40% DSC06437.JPG adapresize40.jpg
And here is the result
$ ls -l
total 2536
-rw-r--r-- 1 imtiaz users  350066 2010-01-18 16:03 adapresize40.jpg
-rw-r--r-- 1 imtiaz users 1592974 2010-01-18 16:00 DSC06437.JPG
-rw-r--r-- 1 imtiaz users  330641 2010-01-18 16:02 resize40.jpg
-rw-r--r-- 1 imtiaz users  319089 2010-01-18 16:00 scale40.jpg
$ identify DSC06437.JPG 
DSC06437.JPG JPEG 2592x1944 2592x1944+0+0 8-bit DirectClass 1.519MiB 0.000u 0:00.000
$ identify DSC06437.jpg 
DSC06437.jpg JPEG 1037x778 1037x778+0+0 8-bit DirectClass 312KiB 0.000u 0:00.000
Here is the script I used to do the whole job in one go. Took only 42 seconds for 71 pictures.
for img in `echo *.JPG`; do
  newname=`echo $img | awk -F. '{print $1}'`;
  convert -scale 40%x40% $img output/$newname.jpg;
See for yourself a SCALEd one @Flickr, click "All Sizes" for the RAW size.


  • shouman

    very nice...I know this thing now from ur post.Thnx brother.

  • Monjur

    Nice thing to know imtiaz bhai. I was interested and wrote a batch script for windows using image magick that will serve the same purpose.

    imagecompressor.bat // by ImageMagick
    @echo off
    rem my image magick installation directory
    set imagicdir=E:\ImageMagick-6.5.9-Q16-windows\ImageMagick-6.5.9-0
    rem my input image directory
    set mydir=E:\images
    rem my output directory
    set outdir=E:\output
    cd %mydir%
    for %%a in (*.jpg) do (
    %imagicdir%\convert.exe -scale 40%x40% %%a %outdir%\%%a

    Hope it will help windows users.

  • Post a Comment