ImageMagick in Examples
Oct 18 2006
I have a ‘love and hate’ relationship with Imagemagick. On one hand it can be a time saver and supports a plentitude of file formats. On the other the documentation wasn’t written for mortals like myself and each version introduces 3 bugs for every fixed one. I have wasted quite a lot of time figuring why my scripts randomly start failing at different tasks and usually kept at least two versions of Imagemagick handy.
This unfortunate situation has been somewhat fixed with the emergence of Graphicsmagick. GM seems to give expectable results every time.
Here is a few use example uses I keep needing over and over. I was actually more succesful with getting help from google rather than GM/IM’s man pages. I hope this will be useful for other people and they won’t need to waste as much time as I have in the past. Feel free to post your common usage in the comments.
Adding margin
To add some transparent margin around a graphic:
convert -bordercolor Transparent -border 1x1 in.png out.png
Image Tiles
Create a grid of tiled images of 32x32px:
montage -background Transparent -tile 8x4 -geometry 32x32 00*png tiles.png
Overlay Image
To alpha compose an image over another (onto bottom left):
composite above.png -compose Over -gravity SouthEast under.png result.png
Cropping image
Cropping is a bit confusing as what I understand under crop is called shave in IM. Crop appears to only be useful when applied in a sequence of passes. I’d kinda expect to define a rectangle for crop, but…
shave XxY
(will crop X pixels from both sides and Y pixels from top and bottom)crop 400x300+40-50
(crops 40px on the left and 50 pixels from the bottom on a 400x300 image)
Flatten onto White
To discard alpha and flatten an image onto a white color:
composite -compose Dst_Over -tile xc:white rgba.png rgb-white.png
Chop Tiles into Frames
To convert a tiled image (single image animation composed of 32x32px tiles) into individual frame files:
convert tiles.png -crop 32x32 -repage 0x0+0+0 frame_%02d.png
In future I will probably post some sort of a tutorial for Blender’s compositor which is gaining more and more functionality and has a fairly nice graph editor UI.