How to Use Convert Tool to Edit Images from Linux Command Line

How to Use Convert Tool to Edit Images from Linux Command Line

ImageMagick is a suite of image modification software for Linux. It consists of many tools with number of options for image modification, conversion, etc.

Installing ImageMagick (convert)

First verify if ImageMagick is already installed using:

convert -version

If it is not installed, we can install it on Ubuntu and Debian with:

sudo apt install imagemagick

Note: For older Ubuntu versions (version 14.04 and below), you need to use apt-get instead of apt.

To install on CentOS and Fedora, run:

yum install ImageMagick

Resize an Image using Convert

To resize an image, we use the flag -resize:

convert test.png -resize 300x200 test_2.png
# Here test.png is the source image, test_2.png is the name for converted image
# 300 is the width to be converted to in pixels, and 200 is the height in pixels

convert test.png -resize 300 test_2.png
# This keeps the height but changes width to 300

convert test.png -resize x200 test_2.png
# This keeps the width but changes height to 200

Convert Image Format

The convert tool can convert images from one image format to the other. It supports huge number of formats.

Below is an example command to convert a PNG image to JPG format.

convert test.png test.jpg

Change Image Brightness and Contrast

Convert can be used to modify attributes such as brightness, contrast, compression level, etc. of an Image, similar to GUI based tools.

To change image brightness, use:

convert -brightness-contrast 10 test.png test_2.png

To change contrast level of an image, use :

convert -brightness-contrast x5 test.png test_2.png

To change the quality index (compression level) of a JPEG image, use:

convert test.jpg -quality 15 test_2.jpg

Note: Lower compression level means better quality of image. And of course, larger image size too as compared to high compression levels.

In a similar way, other attributes can be modified. Almost all the tasks which can be performed using usual GUI tools can be done using ImageMagick’s convert command line tool.

For a complete list of all the things convert command can do, see the convert man page. Or, run the following command in your terminal.

convert man

🍻 Cheers!