Converting and manipulating image files on the Linux command line

Using the convert command, you can modify image files without having to open an image editor.

Converting and manipulating image files on the Linux command line
Sandra Henry-Stocker

Most of us probably know how wonderful a tool Gimp is for editing images, but have you ever thought about manipulating image files on the command line? If not, let me introduce you to the convert command. It easily coverts files from one image format to another and allows you to perform many other image manipulation tasks, as well — and in a lot less time than it would take to make these changes uses desktop tools.

Let's look at some simple examples of how you can make it work for you.

Converting files by image type

Coverting an image from one format to another is extremely easy with the convert command. Just use a convert command like the one in this example:

$ convert arrow.jpg arrow.png

The arrow.png image should look the same as the original arrow.jpg file, but the file will have the specified file extension and be different in size. The convert command will use the file name you provide (in this case, arrow.png) to determine what type of file you want to end up with and reformat the image accordingly. If you doubt that the proper conversion could possibly happen as quickly and easily as it does, you can always use the od command to examine the beginning of each of your files.

od -bc arrow.jpg | head -6
0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 000 000 001
        377 330 377 340  \0 020   J   F   I   F  \0 001 001  \0  \0 001
0000020 000 001 000 000 377 333 000 103 000 003 002 002 002 002 002 003
         \0 001  \0  \0 377 333  \0   C  \0 003 002 002 002 002 002 003
0000040 002 002 002 003 003 003 003 004 006 004 004 004 004 004 010 006
        002 002 002 003 003 003 003 004 006 004 004 004 004 004  \b 006
od -bc arrow.png | head -6
0000000 211 120 116 107 015 012 032 012 000 000 000 015 111 110 104 122
        211   P   N   G  \r  \n 032  \n  \0  \0  \0  \r   I   H   D   R
0000020 000 000 001 364 000 000 001 167 010 000 000 000 000 166 231 346
         \0  \0 001 364  \0  \0 001   w  \b  \0  \0  \0  \0   v 231 346
0000040 040 000 000 000 004 147 101 115 101 000 000 261 217 013 374 141
             \0  \0  \0 004   g   A   M   A  \0  \0 261 217  \v 374   a

Yes, convert did all that in a few seconds. The "JFIF" string displayed in the first file and "PNG" displayed in the other verify that the operation performed as instructed. These are the internal codes used to identify these two file formats.

Reformatting large groups of files

If you want to use the convert command to generate hundreds of .jpg files from hundreds of .png files (or vice versa), you can use a script like this. Depending on the number of files to be converted, it still will likely take only take a matter of seconds or possibly minutes to complete the task.


for file in `ls *.png`
    newfile=`echo $file | sed 's/png/jpg/'`
    convert $file $newfile

Note: Use sed 's/jpg/png/' if you want to convert jpg files to .png format.

While I'm only showing .png and .jpg files in these examples, convert will work with other image types as well (e.g., .bmp, .gif and .tif).

Rotating and flipping

To rotate a file, you need to tell convert how many degrees to rotate it. The first of the two commands below will rotate an image 45 degrees in a clockward direction. The second uses the -flip option to turn it upside down.

$ convert arrow.jpg -rotate 45 arrow45.png
$ convert Sandra_Simpson.png -flip Sandra_flipped.png

Notice that in the first command we're doing the rotation and the conversion to a .png file with a single command.

shenrystocker Sandra Henry-Stocker

Keep in mind that a 180-degree rotate and a flip are not the same thing. The rotate command will create an image that looks like you've spun the original image around halfway. The flip will create mirror image of the original image.


To add annotation to an image, use a command like this one, but make sure the font size works for your image. Note that you should select a text color that will show up on your image and that the size and position of the text must be specified.

shenrystocker cartoon annotated Sandra Henry-Stocker

The pointsize setting should depend on the size of your image file. The coordinates are relative to the upper left corner of the image. Experiment with the settings, and you're sure to find some that work well for you.

$ convert Sandra_Simpson.jpg -font courier -fill black -pointsize 50 -annotate +50+50 'My Simpsons Look' Sandra_annotated.jpg


In this command, we're appending two images together to form a single image.

$ convert Sandra_Simpson.png Sandra_flipped.png -append appended.png

The original and flipped images are appended — one on top of the other.

shenrystocker cartoon Sandra Henry-Stocker

Many other options ...

The Convert - Linux man page says the command can "convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more" — and that's no exaggeration. While the basic operations are extremely fast and easy to perform, the huge variety of image manipulation options is somewhat mind boggling. Install the program and experiment with some sample images, and you'll slowly discover many options that you will enjoy using.


Once you decide what kind of image manipulation you want to perform and work out the command that makes the intended changes for you, you can run through a set of files with surprising speed — much faster than making the same type of changes using Gimp. The convert command is a real winner for the "no artistic skills needed" kind of image manipulation.

I hope you enjoy working with the convert  command. And now you know what I'd look like as a Simpson!


Copyright © 2018 IDG Communications, Inc.

The 10 most powerful companies in enterprise networking 2022