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
Figure 3: With the correct parameters, convert creates images like this Linux Pro logo.

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).

Figure 4: The display tool presents images and provides editing options.

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 \
Figure 5: The font specimen created from the LiberationMono Italic TrueType font using convert.

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).

Figure 6: Two images with offset yellow dots and the result of their comparison.

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):

Figure 7: The composite command combines photos, with plenty of scope for experimenting.
$ 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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Tweak Images with Converseen
  • Image Wizardry

    The free ImageMagick graphics toolbox brings the feature set of a full-blown image processor to the command line.

  • ImageMagick

    GIMP isn’t the only option for photo manipulation. ImageMagick, a collection of command-line programs for image processing, can help you process multiple images in one go.

  • Tutorial – ImageMagick

    ImageMagick can do more than just edit existing images. The free software can even be scripted to create simple drawings.

  • ExactImage

    Automate image processing with this quick and easy-to-use photo tool suite.

comments powered by Disqus