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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.