Monday, 20 February 2012

Batch converting images files from one format to another using ImageMagick

Let's say you have 500 JPG images and want to convert them all to PNG in one go. The mogrify function in the ImageMagick software suite comes in handy here:

mogrify -format png *.jpg

This produces a copy of each image in the new format. It's possible to combine the conversion with other operations, for example resizing and conversion to monochrome:

mogrify -resize 50% -monochrome -format png *.jpg

I often create PDF vector graphics which I use together with PDFLaTeX. However, sometimes I need bitmap versions of the vector graphics images. Mogrify can convert from PDF to bitmap formats (for example PNG), but it's important to set the density of the sampling grid in the conversion (dots per inch), otherwise the bitmap image might have a too low resolution. Example:

mogrify -format png -density 600 *.pdf

The commands above can be executed directly in the terminal if you are inside the folder containing the images. The folder can also be specified explicitly, for example

mogrify -format png /home/martin/images/*.jpg