When it comes to converting or editing image files in Linux, ImageMagick is undoubtedly one of the most well-known integrated software. It contains a complete set of command-line tools to display, convert, or copy raster or vector image files of more than 200 types, all at the command line. ImageMagick can be used for a variety of image editing tasks, such as converting file formats, adding special effects, adding text, and changing images (resizing, rotating, flipping, cropping).
If you want to trim the image to remove whitespace, you can use the two command-line tools that ImageMagick comes with. If you have not installed ImageMagick, please refer to this guide to install it.
In this tutorial, let's trim the following PNG image. We want to remove the right and bottom edges of the image so that the icon is centered.
First, identify the size (width and height) of the image file. You can use the identity command to do this.
- $ 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 the two things you want to do with the image clipping: (1) The position at which the cropped image begins (2) the size of the clipped rectangular area.
In this example, let's assume that the image clipping starts at the upper-left corner and that the more precise point is in x=20px and y=10px, so the cropped image size is 1200x700px.
The tool used to crop the image is convert. With the-crop option, the convert command trims a rectangular area in the input image.
- $ convert Chart. PNG -crop 1200x700++ Chart-cropped. PNG
Specifying the input image as the Chart.png,convert command stores the cropped image as Chart-cropped.png.
How to trim an image on the Linux command line