ImageMagick Cookbook

My own quick reference for using ImageMagick in specific tasks.

IMPORTANT: the order of the parameters does matter! Operations are applied in order.

Convert a DDS texture to PNG

magick convert source.dds destination.png

Valid for any combination of formats.

Extract DDS layers to individual PNGs

magick convert source.psd -set dispose Background -coalesce destination.png

Writes a sequence of files named destination-0.png, destination-1.png, destination-2.png, etc. 0 is the composed image, 1 is the first layer, 2 the second layer, etc.

source

Batch-convert many images to a different format

magick mogrify -format png *.dds

*.dds are the source images, png is the target format. Valid for any combination of formats.

Extract the Alpha channel from an image

magick convert source.png -set colorspace RGB -alpha extract source_alpha.png

-alpha extract: Copies the alpha channel values into all the color channels and turns ‘Off’ the the image’s transparency, so as to generate a grayscale mask of the image’s shape.

-alpha referencesource

Remove the Alpha channel from an image

magick convert source.png -alpha off destination.png

source

Copy the Alpha channel or grayscale from an image to another

magick convert source.png source_alpha.png -compose copy-opacity -composite PNG32:composed.png

source.png and source_alpha.png should have the same size. Otherwise, the locations are controlled according to -gravity and -geometry settings.

If source_alpha.png doesn’t have an alpha channel then the grayscale values are copied.

-compose copy-*: Copy the specified channel (Red, Green, Blue, Cyan, Magenta, Yellow, Black, or Opacity) in the source image to the same channel in the destination image. If the channel specified does not exist in the source image, (which can only happen for methods, ‘copy-opacity’ or ‘copy-black’) then it is assumed that the source image is a special grayscale channel image of the values that is to be copied.

-composite: Perform alpha composition on two images and an optional mask. Take the first image ‘destination’ and overlay the second ‘source’ image according to the current -compose setting.

-compose and -composite reference | source

Convert a PDF file to PNG

magick convert -density 150 -antialias source.pdf -append -resize 1024x -quality 100 destination.png
  • remove -append to extract each page separately.
  • source.pdf[i] to convert only the specified image.
  • source.pdf[i-j] to convert the page range.
  • Extra high quality: use -density 600 and -resize 8192x

source

Convert images to PDF

magick convert -resize 2048x *.png -quality 100 destination.pdf
  • remove -resize to use the original images.
  • removing -quality reduces the pdf size.

Split an image in vertical slices

magick convert source.png -crop 300 +repage result.png

Multiple images will be written. Last one will be the remainder width.

Modify the canvas size

magick convert source.png -background none -gravity center -extent 1024x1024 result.png
  • The original image is cropped or the background is extended to fit the -extent parameter. Original image is not scaled.
  • use -background <color> for specifying the color that will be applied to the canvas, including all transparent and semi-transparent zones. Default is white.

-extent reference | color list