How to crop an image from the command line on Linux

Question: I would like to get rid of white margins of an image file. Is there an easy way to crop an image file from the command line on Linux?

When it comes to converting or editing images files on Linux, ImageMagick is undoubtedly one of the best known all-in-one image software. It boasts of a suite of command-line tools to display, convert, or manipulate more than 200 types of raster or vector image files, all from the command line. ImageMagick can be used for a variety of image editing tasks, such as converting file format, adding special effects, adding text, and transforming (resize, rotate, flip, crop) images.

If you want to crop an image to trim its margins, you can use two command-line utilities that come with ImageMagick. If you haven't installed ImageMagick, follow this guideline to install it.

In this tutorial, let's crop the following PNG image. We want to get rid of the right and bottom margins of the image, so that the chart will be centered.

First, identify the dimension (width and height) of the image file. You can use identify command for that.

$ identify chart.png
chart.png PNG 1500x1000 1500x1000+0+0 8-bit DirectClass 31.7KB 0.000u 0:00.000

As shown above, the input image is 1500x1000px.

Next, determine two things for image cropping: (1) the position at which the cropped image will start, and (2) the size of the cropped rectangle.

In this example, let's assume that the cropped image starts at top left corner, more specifically at x=20px and y=10px, and that the size of a cropped image will be 1200x700px.

The utility used to crop an image is convert. With "-crop" option, the convert command cuts out a rectangular region of an input image.

$ convert chart.png -crop 1200x700+20+10 chart-cropped.png

Given the input image chart.png, the convert command will store the cropped image as chart-cropped.png.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

One thought on “How to crop an image from the command line on Linux

Leave a comment

Your email address will not be published. Required fields are marked *