Monday 12 November 2012

Batch cropping white space borders in Matlab figures

Matlab figures have, by default, a white space border around them. This is fine if you want to view them by themselves, but wastes space if you want to add them to a text document, where the white borders are "added" to the document margins. It is possible to adjust the border width in Matlab, but if you already have exported the figures to an image file, it's too late. (Note: When exporting to EPS the border is not included by default).

If you want to remove this white space for a large number of image files in one go, ImageMagick is your friend. For example,

mogrify -trim +repage *.png

will remove the white border of all PNG files in the current directory, using the "smart crop" trim option. Consult the ImageMagick documentation on cropping for further options.