How to Convert and Optimize an Image from Linux Command Line

As a technical writer, my job involves researching, experimenting with, and documenting useful software tools. However, there’s one more task that consumes a significant portion of my work time – image editing (yes, those screenshots).

Growing up using Microsoft Paint (like most of us I assume), I tried using some GUI-based Linux image editors in the beginning, only to realize that it’s time consuming, especially given that image editing is not my core work but still needs to be done repeatedly. Thereafter, I gradually shifted my image editing work to the command line, mastering the how-to of basic operations initially, and then learning some advanced tips/tricks once I got myself comfortable.

Well, that’s my story; yours could be different, of course, and could even be in the making. The point here is to make GUI fans believe that work like image editing can also be performed through the command line and that it’s not at all difficult. With that in mind, we’ll be discussing a command line image editing tool – Convert – in this article.

The command line utility “Convert” is a part of the ImageMagic software suite. The creators claim the suite is capable of reading and writing images in a variety of formats (over 200) including PNG, JPEG, JPEG-2000, GIF, TIFF, DPX, EXR, WebP, Postscript, PDF, and SVG, and performing operations like resize, flip, mirror, rotate, distort, shear and transform, and adjust colors.

To install Convert, just install the ImageMagic software suite by running the following command on your terminal:

sudo apt-get install imagemagick

In its most basic form, you can use the Convert command to switch the format of an image. For example, the following command produces “test.jpg” from “test.png.”

convert test.png test.jpg

Resize images

Another important thing that you can easily do with Convert is resize images. For example, the following command takes test.jpg – which is 800×343 in size – and produces test1.jpg with 700×300 size.

convert test.jpg -resize 700x300 test1.jpg

It’s worth mentioning that Convert tries to preserve the aspect ratio by default. For example, if you try to resize “test.jpg” to a file with size 300×300, the resulting file will instead be 300×129 pixels. However, if you still want, you can force Convert to use the size you’ve given to it as input. You can do this by adding an exclamation mark immediately after the size parameter in the command like in the following example:

convert test.jpg -resize 300x300! test1.jpg

Reduce quality/size

Next up is quality of images. Yes, using Convert, you can play with the quality of images as well. For example, when you take a screenshot on your system, it’s generally captured as a png file in a large size. To reduce the size, you can reduce the screenshot’s quality and convert it into a jpg file.

Here’s an example png file that is over 230kb.

convert-screenshot

Now, if you want to convert this into a jpg file that is less than 100kb, here’s the command for it:

convert screenshot.png -quality 80 screenshot-new.jpg

The following is the jpg file produced by the above command.

convert-screenshot-new

There isn’t much of a visible quality reduction, is there? And it’s only 48kb in size.

Create GIFs

Now, suppose you have a bunch of jpg files, and the task is to create a GIF. It’s easy using Convert:

convert *.jpg final.gif

The command above will pick up all the jpg files in the current directory and create a “final.gif” file in the same directory.

Rotation and other effects

You can also rotate images easily using Convert. All you have to do is use the -rotate option along with the number of degrees you want the image to rotate. For example, the following command creates a 90-degree rotated “screenshot.jpg:”

convert screenshot.jpg -rotate 90 screenshot.jpg

convert-screenshot-rotate

Aside from rotation, you can also add other effects to your images. For example, the following command adds an “implode” effect:

convert screenshot.jpg -implode 1 screenshot-implode.jpg

It results in the following image.

convert-screenshot-implode

While the examples discussed in this article should be enough to give you a good idea how Convert works (as well as how easy it is to do image editing from the command line), the fact is that we’ve barely scratched the surface here. For more details, take a look at Convert’s man page where you’ll find a plethora of options and features. Additionally, you can also go through ImageMagic’s official website – it’s a good resource as well.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.