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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.