ImageMagick - Manipulate images from the command line

mail

How to join several images into one ?

Join images horizontally :
convert +append image1 image2 image3 resultImage
Gives :
image1 image2 image3
Join images vertically :
convert -append image1 image2 image3 resultImage
Gives :
image1
image2
image3

Example for Bash lovers :

This image was generated by makeMosaic.sh.
mail

identify

mail

mogrify

mogrify itself has plenty of sub-commands :

resize :

  • mogrify -resize 50% image.jpg
  • mogrify -resize widthxheight image.jpg
    width and height are maximum values, the aspect ratio is preserved (full details)

rotate :

mogrify -rotate degrees image.jpg
rotates clockwise when degrees > 0.
mail

convert

Change image format :

convert sourceImage.png destinationImage.jpg
mail

annotate

mail

Image processing with ImageMagick

GraphicsMagick looks like a good alternative to ImageMagick (not tried yet).

Get help :

ImageMagick is a set of binaries that allow image manipulation (examples) via the command line :
  • animate
  • annotate : add text to an image
  • composite : overlap one image over another
  • convert : convert file formats
  • display : display and manipulate image under X.
  • draw
  • identify fileName : outputs the image definition, color depth, file type and size, ...
    • for FULL details, use the -verbose flag
    • to view EXIF metadata (source) : identify -format '%[EXIF:*]' photo.jpg
  • import : capture screenshot
  • mogrify : transform image (scale, rotation, color, ...)
  • montage
  • xtp
man is available for all of these commands.

Resize a picture

mogrify -geometry width%xheight% fileName
The picture width and height are respectively multiplied by the width% and height% parameters, which can be less or greater than 100%.
mogrify -resize widthxheight fileName
The image will be resized - keeping its aspect ratio - within the limits of the specified width and height values.
mogrify -resize 640x480! file.jpg
To strictly resize an image to a given size, just use the "!" flag.

Annotate images with a text tag (source) :

This can be done with the annotate or convert + draw commands :
option usage
pointsize font size in points
draw annotate the image with a graphic primitive
gravity horizontal and vertical text placement
fill color to use when filling a graphic primitive
text x,y string print string in a containing box having its upper left corner at x,y. (conflicts with gravity ???)
font specify the font name. List available fonts with convert -list type (version < 6.3.6), or convert -list font (later versions)

convert originalImage -pointsize 30 -draw "gravity SouthEast fill white text 0,12 'tagText' " annotatedImage

To tag (and overwrite) all images of a directory :

for img in *; do convert $img -pointsize 30 -draw "gravity SouthEast fill white text 0,12 'tagText' " $img; done

Resize + re-orient + watermark images :

maxWidth=800; maxHeight=$maxWidth; watermarkFile="path/to/watermark.png"; for img in $(ls | egrep "jpg,JPG"); do echo $img; mogrify -resize ${maxWidth}x${maxHeight} $img; composite -dissolve 80% -gravity SouthEast -quality 100 $watermarkFile $img $img; done

To re-orient images, add in the process : convert -auto-orient originalImage reorientedImage