rekowski.info David Rekowski's random stuff

Imagemagick notes and examples

Updated 2012-12-30

Keywords

imagemagick, tutorial, demo, example, how-to

1. Take a screenshot (Linux)

$ import -window root screenshot.png

You can delay the screenshot taking by calling sleep beforehand with the number of seconds to wait, e.g. 5:

$ sleep 5 && import -window root screenshot.png

Scale images e.g. to 1024x768

You can convert all jpgs in the current folder to fit into a imaginary 1024x1024 box while retaining the aspect ratio. Target filename is thumb_<originalname>.

$ for i in *.jpg; do convert "$i" -scale 1024x1024 "thumb_$i"; done;

Also useful with a different folder:

$ mkdir thumbs
$ for i in *.jpg; do convert "$i" -scale 1024x1024 "thumbs/$i"; done;

PDF preview image

You can create a 200px wide preview image of all pages of a PDF like this:

$ convert -geometry 200x somefile.pdf somefile.png

If you want a specific page, e.g. the frontpage (index 0) postfix the pdf filename:

$ convert -geometry 200x somefile.pdf[0] somefile.png

Set the JPEG quality

You can force a specific quality setting for JPEGs:

$ convert -quality 90 -geometry x200 input.jpg output.jpg

As far as I know, geometry is more versatile than scale, see geometry documentation.

Set transparency when using -fill

You can amend the RGB hex code by a transparency setting, e.g. 003399CC where CC is the transparency value.

Example:

$ convert -fill "#3366FF33" -stroke "#00000033" -pointsize 60 \
  -family Verdana -weight bold -draw "text 30,150 'see me'" \
  -size 300x300 xc:transparent -fill "#CC000066" \
  -stroke "#0000CC66" -draw "rectangle 120,30 270,270" test.png

Result:

result of command

create PDF contact sheet

Sometimes it can be useful to have a single image representation of a PDF file. This can be achieved like this:

$ montage file.pdf -colorspace RGB -geometry 200x300+16+16 output.jpg

The colorspace option prevents black backgrounds with transparent portions of a PDF. The geometry reads <width>x<height>+<offsetX>+<offsetY> and determines the size and space for each image tile in the resulting image. Reduce it to e.g. 100x150 for larger PDF files.

Combined with a few pieces of bash, you can create a <pdf-file>.jpg for every pdf in the current directory and all subdirectories:

$ find -f * | egrep -i ".pdf$" | while read f; do montage "$f" -colorspace RGB -geometry 200x300+16+16 "$f.jpg"; done;

Result (scaled down; click for actual, larger version)

PDF contact sheet index image

Mac OS X note:

I actually had the issue that only the first page would be rendered. This could be corrected by editing the delegates.xml changing pngalpha to pnmraw as stated in this article on stackoverflow. The location of this file can be obtained by calling

$ convert -list configure | grep CONFIGURE_PATH

which in my case points to /usr/local/Cellar/imagemagick/6.8.0-10/etc/ImageMagick/, since I installed imagemagick via homebrew.