5 common image manipulation tricks using ImageMagick
November 15th, 2009 in ImageMagick, Shell Scripting. Add commentWebmasters/Webdesigners often need to do repetitive image manipulation tasks like resizing for thumbnails, image borders etc. Even though a plethora of image manipulation software are available, nothing beats ImageMagick when you need to perform simple tasks, especially since it lends itself well to automation using shell scripts. We outline here 5 common image manipulation techniques with ImageMagick. The scripts have been tested with ImageMagick version 6.4.5.4.
-
Create thumbnails (i.e resize images):
The following resizes a picture (given by picturename) to a 62×47 image called picturename_thumb.convert pitcurename -resize 62×47! picturename_thumb;
Note the “!” after the 47. By default, ImageMagick maintains the aspect ratio while resizing, so if you omit the !, the height may not be 47px. Adding the ! forces the height to that specified.
You can automate the above to resize a bunch of images using a shell script. For example, the following script will resize all .jpg images in the current directory, append a _thumb to the resized images and put them in a directory called thumbs.for photos in `ls *.jpg`;
do base=`basename $photos .jpg`;
convert $photos -resize 62×47! thumbs/”$base”_thumb.gif;
doneNote: For the above script to work your OS must support basename. You can use the above script as a template for automating the other ImageMagick tricks described here.
-
Add a border to an image
Add a 2 pixel border (with color #00FF7F) to photo.jpg and call it framed.jpgconvert photo.jpg -bordercolor ‘#00FF7F’ -border 2 framed.jpg
-
Convert an image to black & white
convert -type Grayscale main2.jpg main2_bw.jpg
-
Add a label in the image
The following will add the label ‘Copyright © John Doe’ at the bottom of the imageconvert myimage.jpg -gravity south -fill white -annotate 0 ‘Copyright: © John Doe’ myimage_labeled.jpg
Use the gravity attribute to specify the position of the label. Besides the four obvious directions you can also specify SouthEast, NorthEast etc.
Note: To add the © symbol from the command line terminal, press down ALT key and type 0169 on the NUM keypad. -
Add a drop shadow
convert picture.jpg \( +clone -background black -shadow 80×3+5+5 \) +swap -background white -layers merge +repage shadow.jpg
Vary the numbers(80×3+5+5) to change the shadow weight, direction etc.
Tags: Add new tag, Shell Scripting

