Professional graphics and photo editing at the command line

Tutorial – ImageMagick

Article from Issue 269/2023
Author(s):

The free ImageMagick graphics toolbox brings the feature set of a full-blown image processor to the command line.

For more than 30 years, a group of about 30 people has been developing the ImageMagick [1] graphics toolbox and offering it for free under the Apache 2 license. It is not a single program, but a set of compact tools for the command line. In addition to a Linux version, there are variants for Windows, macOS, iOS, Android, and other operating systems. ImageMagick supports over 200 file formats and reliably processes images with resolutions in the gigapixel and terapixel range.

You can install the program collection using your choice of distribution's package manager. At the time of going to press, only Arch Linux had the latest 7.1 version on board. Debian 11 (including Testing and Unstable) and Fedora 37 still offered version 6.9. On Debian-based systems such as Ubuntu and Linux Mint, you need to use the command from line 2 of Listing 1 to install. On Fedora, use the command from line 4. On Arch-based systems such as Manjaro, set up ImageMagick using the command from line 6.

Listing 1

Installation

 

After doing so, you will find the command-line programs listed in Table 1 on your computer. Many instructions on the Internet still refer to old commands where users always had to type magick followed by the command string. The newer versions have dropped this, meaning that magick convert has been simplified to a plain convert.

Table 1

Tool Overview

Command

Function

convert

Main tool for converting, scaling, blurring, sharpening, cropping, rotating, and more

mogrify

Like convert, but replaces the source file

identify

Reads out various image parameters including the Exif data

composite

Overlaps images

assembly

Combines multiple images into one target file

compare

Marks differences between images in the destination file or outputs them as values

stream

Copies pixel data from one image to another

display

Displays images via an X server as a pop-up

import

Creates screenshots in the X server

conjure

Runs scripts in the Magick Scripting Language

Everyday Commands

You can use the identify IMAGE FILE command to initially identify an image without opening it outside the terminal with your distribution's file manager, or you can use the display command. The output then looks like line 2 of Listing 2, for example. If you want to limit the output to the height, width, and file names, add options to the command as shown in line 3; \n stands for a line break here. This is how you can create photo lists for admin purposes, for example. The command from line 4 appends the image data to the TARGET.txt file.

Listing 2

Retrieve Image Data

 

Converting and More

You can use the versatile convert command for converting images to another file format, renaming, cropping, and resizing, as in:

convert SOURCE TARGET

This always creates a new target file, leaving the original image untouched. The counterpart mogrify uses almost identical syntax, but processes the source file directly.

If needed, you can convert from one file format to another and rename the file at the same time. To change only the format, use:

convert image.jpg image.png

If you only want to change the file name, type:

convert image.jpg photo.jpg

In combination the command would be, say:

convert image.jpg photo.png

If needed, you can also convert all files of a certain type in a single action. There are several possible approaches to choose from. The command

convert *.jpg *.png

converts all JPEG files to the PNG target format, but without using the names. The target files are then named *-1.png, *-2.png, and so on. However, you can easily change this with terminal commands (Listing 3).

Listing 3

Mass Rename

 

If you want to rotate or resize images, mogrify often proves useful because it converts the source file directly. For example, if all the images in a folder are portrait but should be displayed in landscape format, you can rotate them all clockwise in one fell swoop using the command

mogrify -rotate "90" *.png

or the other way around using

mogrify -rotate "-90" *.png

Parameters such as -geometry 800x600 or -resize 50% are used to reduce the size of the images.

You can create thumbnails of all the JPEGs in a folder using a batch command such as the one in line 1 of Listing 4. It names the target files preview-1.jpg, preview-2.jpg, and so on. If you want to take the file name and save it in a separate folder (e.g., thumbs/), the command might look like the one in line 2. However, to do this, you first need to create the thumbs/ folder. If you only want to edit the dimensions of an image page, enter, say, 200x or x200 after --resize instead of 200x200. If you use the -thumbnail parameter instead of -resize, convert automatically deletes comments and color profiles from the metadata (line 3).

Listing 4

Create Thumbnails

 

You can convert image sections into animations in a similar way. To create an animated GIF from a set of JPEG files, use the command from line 4 of Listing 4. Enter the value after -delay in hundredths of a second. It sets the time between image changes. If you specify 100, as in the example, a new image appears every second.

Effects and Compression

You can do more with convert and mogrify, such as using them to integrate image effects directly. You can use -blur RADIUS to apply a blur with the specified radius (Figure 1). If you want your image to look more artistic, -charcoal FACTOR, gives you a charcoal effect (Figure 2). To simulate a colored pencil drawing instead, use -sketch RADIUS (Figure 3).

Figure 1: To apply a blur, use -blur RADIUS.
Figure 2: The charcoal pencil effect combines well with a soft focus.
Figure 3: This sketch effect needs further refinement.

If you want to compress your images to save storage space, there are 25 algorithms to choose from as -compress TYPE options. They include bzip2, FAX, JPEG 2000, Lossless, WebP, and many others.

Use -contrast or +contrast to increase or decrease the contrast of the target image. The -sharpen RADIUS command increases the sharpness, with 0x1 being a good starting value. You can change the value on both sides of the x until you achieve the desired results. To surround your image with a frame, use

-border 1x1 -bordercolor 0xFF0000

to create a red frame with a thickness of one pixel.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News