Image editing with command-line tools

On the Line

© Lead Image © Iryna Denysova, 123RF.com

© Lead Image © Iryna Denysova, 123RF.com

Article from Issue 177/2015
Author(s): , Author(s):

Powerful command-line tools offer fast and easy image editing.

When you want to scale, filter, or transform a number of images in the same way, you don't want to open each file in your preferred graphics editor, apply the transformations, then save the new version of the image. Instead, the entire process is far easier at the command line using ImageMagick [1], which is available in the repositories of all distributions. For example, using the command

$ convert -scale 50% image.jpg small/image.jpg

reduces the height and width of image.jpg by 50 percent and writes the results to a file of the same name in the small subdirectory.

Converting one image in this way might not seem so amazing, but by using the shell, you can apply this command very efficiently to many files. The more files you have to transform, the more time savings you realize.

Multiplier

Command-line tools demonstrate their strengths in collaboration with the shell by processing many files consecutively using the same command. Usually, your images will be named image001.jpg to image999.jpg all in one directory. In the simplest case, a for loop expands the wildcard in image*.jpg into a list of all images, which are then processed consecutively in the loop:

$ for i in image*.jpg; do convert -scale 50% "$i" "small/$i.jpg"; done

Adding an echo image*.jpg command would display the intermediate results. The quotation marks ensure that the command can convert files with names containing special characters, such as blank spaces.

With very large photo collections, for which the file names written one after another comprise more than 131,072 characters, you would receive the error message Argument list too long.

To circumvent such problems, the command

$ find . -maxdepth 1 -name "image*.jpg" -exec convert {} small/{} \;

searches the current directory (find .), without entering subdirectories (-maxdepth **1), for files that match the example image*.jpg and then runs the convert {} small/{} command on each file. The curly brackets serve as a kind of placeholder for the current image, and the semicolon (which must be escaped with a backslash) is the end marker for the command. This combination can convert any number of files.

However, when editing a very large number of images, both methods have one drawback: They convert graphics successively, using only a single core on a likely multicore CPU. GNU Parallel (you might need to install the gnu_parallel package using your package manager) can help:

$ find . -maxdepth 1 -name "image*.jpg" | parallel convert {} small/{}

The list of files from the find command is piped to parallel, which then automatically tasks each core with a convert job. To see the savings, compare the CPU utilization from top for the two methods when processing a long list of images. More information about using GNU Parallel can be found online [2].

Photo Montages and Web Optimization

An image montage often provides a nice summary of a subject – whether of family faces from the last outing or screenshots from a film. For ImageMagick to arrange a set of images well, they all need to be the same size:

$ montage image1.jpg image2.jpg image3.jpg image4.jpg -geometry +2+2 montage.jpg

All of the input files are listed first, then the -geometry option puts a small amount of space between the images, and the output file holds the combined images (Figure 1). Because websites often only let you upload individual images, combining several images with montage lets you work around that annoying restriction.

Figure 1: The montage command combines a number of images.

Labeling Photos

The convert command can do a lot more than scale an image and convert one graphic format into another. For example, a picture might be worth a thousand words, but a labeled photo can be even more descriptive. Labeling is very easy to accomplish with the convert command (Listing 1).

Listing 1

Labeling a Photo

$ convert penguin_raw.png -gravity North -font /usr/share/fonts/truetype/\
  LiberationSans-Regular.ttf -pointsize 100 -stroke '#0000A0' \
  -strokewidth 5 -annotate 0 'Tux on Tour' tux_on_tour_simple.png

The -gravity option indicates the area of the photo to which the text is attracted. The text travels upward if you specify North and thus appears at the top of the screen. Entering

convert -list gravity

lists all possible values for -gravity, including the corners (e.g., SouthEast) or the centering parameter (i.e., Center).

The -annotate option then adds the desired text to the image. In doing so, it expects a numeric value, which specifies the angle of rotation; the text then follows. To insert a line break within your text, use \n. The software also lets you name the desired font as a complete path name. The supported font names can be shown using:

convert -list font

The results from the command in Listing  1 would look rather boring. Fortunately, almost any number of options can be combined. For example, the tool can write text in several colors, and you can change the border color or the border thickness.

Figure 2 shows the results after the command in Listing 2 is run on an unlabeled image.

Listing 2

Example Labels

$ convert penguin_raw.png -gravity North -font Liberation-Sans \
  -pointsize 100 -stroke '#000000' -strokewidth 20 -annotate 0 \
  'Tux on Tour' -stroke '#0000A0' -strokewidth 10 -annotate 0 \
  'Tux on Tour' -stroke none -fill '#A0A0FF' -annotate 0 'Tux on Tour' \
  -gravity South -pointsize 90 -stroke '#000000' -strokewidth 30 \
  -annotate 0 'Issue 177' -stroke '#A0A0A0' -strokewidth 15 -annotate 0 \
  'Issue 177' -stroke none -fill white -annotate 0 'Issue 177' tux_on_tour.png
Figure 2: The ImageMagick tool convert adds informative captions to an image.

Buy this article as PDF

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

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News

njobs Europe
What:
Where:
Country:
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia