Image editing with command-line tools
Editing PDF Files
PDF files are practical in several ways. First, virtually all modern operating systems support the format. Second, many graphics programs have rather complex printing functions, whereas PDF viewers are usually much simpler to figure out. Even inexperienced users can print their PDF picture albums with a single click. With ImageMagick, you can create a PDF photo album on the fly:
$ convert *.jpg photo_album.pdf
Conversely, web applications often need individual images. The following command converts a PDF document into individual images that can then be integrated into an existing image gallery:
$ convert user_guide.pdf user_guide-%04d.png
In this case, the %04d
string indicates the naming scheme for generated images (i.e., four-digit numbers with leading zeros).
Instead of editing an existing image file, convert
can also create a new image with a given area of a certain size and color. The command in Listing 3, for example, draws a Linux Pro logo (Figure 3). The ImageMagick documentation describes the tool's extensive drawing skills, such as color shifts, random images, diverse mathematical operations, and transparencies [3].
Listing 3
Creating an Image from Scratch
$ convert -size 270x100 xc:black -gravity West -font Arial-Bold \ -pointsize 80 -fill white -annotate 0 'LINUX' -rotate -90 \ -gravity North -font Arial-Bold -pointsize 30 -fill red \ -annotate 0 'PRO' -rotate 90 linuxpro.png
Finally, the software even lets you edit videos without sound by breaking them down into individual images, which you can modify as desired, and then regenerating a video from the images after the corrections have been made. The suite might, however, need the help of external programs such as FFmpeg.
You can see a simple example in Listing 4. The listing also shows another way to transform multiple images: With mogrify
, which is similar to convert
, you can edit multiple images at the same time. Whereas convert
always writes its results to a second file, the mogrify
command always modifies an existing file.
Listing 4
Editing a Video
$ convert a.wmv F-%06d.png $ mogrify -scale 50% -type Grayscale -swirl 180 F*.png $ convert F*.png c.wmv
Mouse Movements
ImageMagick includes two programs that require a mouse. The first is import
, which you can use to grab screenshots. Entering the command
import screenshot.png
lets you select an area with the mouse that you want to capture, or you can capture the content of the entire screen if you add the -window root
option.
The display
tool displays images for editing. Each mouse button performs a function: The left button reveals a simple menu, the middle button zooms in on a section of the image and shows information such as the color and position of an individual pixel, and the right button brings up different context menus in the image windows (Figure 4).
ImageMagick can also treat fonts as graphics. The output of the command in the first line of Listing 5 displays a font specimen for LiberationMono Italic (Figure 5). The command in the second line converts the font into a common graphic format (e.g., to display on a website).
Listing 5
Converting Fonts to Graphics
$ display /usr/share/fonts/truetype/LiberationMono-Italic.ttf $ convert /usr/share/fonts/truetype/LiberationMono-Italic.ttf \ LiberationMono-Italic.png
Advanced Options
The compare
tool can even solve picture puzzles. It compares the two images and selects the pixels that are different. The example in Listing 6 illustrates this using two graphics created using convert
, each with a circle.
Listing 6
Comparing Images
$ convert -size 200x200 xc:white -fill yellow \ -draw 'circle 100,100,50,100' 1.png $ convert -size 200x200 xc:white -fill yellow \ -draw 'circle 130,100,80,100' 2.png $ compare 1.png 2.png imagecompare.png
The first two values from the draw
command specifies the center point, and the next two define a further point on the circle. The circle appears slightly shifted to the right in the second image. The result of the comparison with the compare
command in the last line is two crescents that show the differences (see Figure 6).
The composite
command, on the other hand, allows you to combine images. The following command, for example, transparently adds the previously created self-made Linux Pro logo to the top right of a photo (Figure 7):
$ composite -gravity NorthEast -dissolve 20% -geometry +20+20 \ linuxuser.png penguin_raw.png penguin-with-logo.png
To see information about an image, use identify
and add the -verbose
option to see more. The animate
tool shows several images in succession as a slideshow.
The conjure
program performs operations similar to convert
or mogrify
; however, instead of supporting command-line options, which describe the operations, it gets the information from a script in the XML-based Magick Scripting Language (MSL) [4]. Finally, the stream
tool proves useful in editing very large images by writing a selected section of an image to a separate file.
To get to know all the subtleties of the programs, visit the ImageMagick project page [1] or consult the help function and man pages. The extensive list of supported graphic formats is shown with convert -list format
.
« Previous 1 2 3 Next »
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
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.