Monday, October 19, 2009

How ImageMagick can save you time


ImageMagick is a great tool! Many times it has saved me from programming. Here is just one example of many applications of the ImageMagick that can save one's time.

Recently, I was asked to check whether a set of images (few hundreds of them) contains one particular image. So, basically I was given and image (called testImage.tiff), and I needed to check whether this image belongs to a set a few hundreds of images. En example of the images from the set is below:


So how to anwser this? You can do manual (i.e. visual comparison), you can also write some simple script in Python with PIL. All would take some time. But why to do this, if you can just write one for loop in bash with compare command from ImageMagick?

So first thing I did is to create the following folders:

The input folder contains all images from the set, the output folder will be where the results of my search will be stored, and the testImage.tiff is my test image.

So thanks to ImageMagick's, the only thing I had to do is to execute its compare command on all images from the set (i.e. input folder).  In other words, I compared, in a loop, an image from the set with the testImage.tiff and the results of the comparison went to output folder. Below is the bash for loop that was executed inside the input folder.
for f in *.tiff ; do
compare $f ../testImage.tiff ../output/$f ;
done
After loop finished, I could go to output folder, and quickly find out whether testImage.tiff is in the set:


As can be seen in the above picture, the testImage.tiff belongs this this set.

One limitation of this procedure is that I had to manually go through the output folder. But because all not matching images are in red, it was very quick to find, the one image that was not red. (if any). Additionally, if the testImage.tiff and images from the set were of different size or type additional code would be necessary.

So thanks to the ImageMagick, I could do the job in just a few minutes.

4 comments:

  1. Anonymous10:23 PM

    Interesting, is GraphicsMagick better than ImageMagick?

    ReplyDelete
  2. Anonymous1:16 PM

    yeah usefull , but what if you did it with a md5sum comparaison ?

    ReplyDelete
  3. Yes, ImageMagick is an amazing tool once you get to know and understand it's power! I've been working on quite a large image matching project recently and ImageMagick and Magick++ (the C++ interface) saved me a lot of work...
    But yes, I must agree, for what you tried to do a hash function may have been simpler/better/faster :)

    ReplyDelete
  4. Thanks. I did not think of a hash function. When next time I'll have to do simillar thing I will try md5sum.

    ReplyDelete