Thursday 1 September 2011

Cropping an image from the command line (/Matlab)

I recently had to crop some figures which had a lot of unnecessary white space on the sides. The figures were made in Matlab, and I wanted to find some way to do the cropping from the command line, to avoid manually editing each image.

As always when it comes to image editing, ImageMagick provides a solution, as explained here. The "convert" command is used to crop images with the following syntax:

convert -crop (width)x(height)+(horzOffset)+(vertOffset) infile outfile

where (horzOffset) and (vertOffset) denote the pixel coordinates of the upper left corner of the cropping window (sideways, down). The parentheses are not included in the syntax. Example:

convert -crop 1800x1597+700+0 im1.png imCropped.png

crops im1.png to size 1800x1597, with 700 horizontal offset and 0 pixels vertical offset. To do the same from Matlab, simply use the "system" command:

> system('convert -crop 1800x1597+700+0 im1.png imCropped.png')

0 comments:

Post a Comment