Last updated on July 3, 2020 by Dan Nanni
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.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean