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
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
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.