Saturday, July 18, 2009

Imagemagick - convert (a tool to batch manipulate images)

Convert is an extremely powerful and versatile but "simple to use" tool to manipulate images. No need to open each file and operate on them in GIMP or Photoshop. I helps best when I know exactly what to do with my pics. For example, I want to resize all pics from a recent field trip and also give them a border along with a nice tag at the corner like "Goa trip" so that I can recall later in my life where are those pictures from and if I include the date too(which I always do) I have a nice picture diary. I think it i always a good idea to tag pictures with dates. That is where this tool comes in to be so handy. In Photoshop also you can do batch conversion of images. To do the tagging you have to save a group of transformations like adding text at the corners as "Actions" and open files one by one and keep applying that to each of them. So boring. In imagemagick this is accomplished with just one line command. See my shell script at the end for an example. But will only explain some features which are used regularly not all of them. There are many more. Interested users should refer to the original site which contains extreme details.

convert command popular usages :-

1. Converting Color to Gray-Scale

convert test.png -colorspace Gray gray_colorspace.png

2. Resizing

convert dragon.gif -resize 64x64 resize_dragon.gif

this resizes dragon.gif into 64x64 pixels in size. Aspect ratio may or may not be maintained. Here aspect ratio would be maintained if the original file dragon.gif was 1:1.

or better still just give the width and forget all about aspect ratio,

convert dragon.jpg -resize 640 resize_dragon.jpg

this resizes dragon.gif into 640 pixels in size. Aspect ratio maintained.

3. Converting to other image formats

convert dragonold.bmp dragon.jpg

or in batch mode

convert *.jpg new-*.jpg

This is why I like this command. It can convert to almost anything. Previously I had such a bad time finding the right tools to do the converting and that too for a batch of files. I found that irfanview was the best tool in Windows. But for Linux .... Note that there is an issue about preserving transparencies. Find it in the source site
I found that in batch mode even if I mistype new-*.jpg as just new-8.jpg(missing the wildcard) it automatically renumbers the output images correctly. So cool.

4. Making a pdf out of a group of images

Yes you might guessed it right. It's just.

convert scanofnotes*.jpg notes.pdf

5. Rotating images
convert -rotate 90 image.jpg
This rotates the images CW 90 degress.

Issues :- In case of using wildcards (convert *.jpg new-*.jpg) Imagemagick loads all images to memory before acting on them. It can lead to slowdowns and memory locks. So it is better to do the conversion using shell script than wildcards.

Therefore, I wrote a simple script to do resize pics and then tag them with date and location where the pic was taken.

######### shell scipt #########
for img in `ls *.JPG`

# parse all JPG's in the directory
# note JPG and jpg are treated differently

# resize image from 1280 to 640 pixels and store in temp.jpg so that more transformations could be applied to it
convert $img -resize 640 temp.jpg

# add a border with border command
convert $img -resize 640 -bordercolor "#000000" -border 2x2 -bordercolor "#FFFFFF" -border 10x10 -bordercolor "#000000" -border 4x4 -bordercolor "#FFFFFF" -border 24x24 -bordercolor "#000000" -border 2x2 -font 'Helvetica' -fill white -box '#00770080' border-$img

# add a tag(text) at the lower right corner
convert temp.jpg -gravity southeast -stroke '#000C' -strokewidth 2 -annotate 0 'Party @ GOA (5 July 2008)' -stroke none -fill white -annotate 0 'Party @ GOA (5 July 2008)' tag-$img
rm -f temp.jpg

-border 2x2 : makes a border around a image not on it.
-border 2x2 -bordercolor "#FFFFFF" creates a white border 2 pixels thick around the image.
-gravity southeast : positions the text at the lower right corner
-strokewidth 2 : fix the width
-stroke '#000C' : the color of stroke

Please share your own "convert" recipes in the comments.

Source :

No comments:

Removing audio noise from a video

The basic steps are to extract the audio, remove noise, and then re-insert the noise-free audio back into the video wrapper. Step I: Ext...