Professional graphics and photo editing at the command line
Tutorial – ImageMagick
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
01 ### Debian and derivatives 02 $ sudo apt install imagemagick 03 ### Fedora 04 $ sudo dnf install imagemagick 05 ### Arch Linux and derivatives 06 $ sudo pacman -S imagemagick
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 |
|
Main tool for converting, scaling, blurring, sharpening, cropping, rotating, and more |
|
Like |
|
Reads out various image parameters including the Exif data |
|
Overlaps images |
|
Combines multiple images into one target file |
|
Marks differences between images in the destination file or outputs them as values |
|
Copies pixel data from one image to another |
|
Displays images via an X server as a pop-up |
|
Creates screenshots in the X server |
|
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
01 $ identify image.jpg 02 image.jpg JPEG 811x664 811x664+0+0 8-bit sRGB 62201B 0.000u 0:00.000 03 $ identify -format "%f width: %w Height: %h Format: %m \n" image.jpg 04 $ identify *.jpg >> TARGET.txt
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
$ for i in *.jpg; do \ convert "$i" "${i%.jpg}.png"; \ done
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
01 $ convert *.jpg -resize 200x200 preview.jpg 02 $ for i in *.jpg ; do convert "$i" -resize 200x200 "thumbs/$i"; done 03 $ convert *.jpg -thumbnail 200x200 preview.jpg 04 $ convert -delay 100 *.jpg animation.gif
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).
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.