Image editing with command-line tools
On the Line
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.
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
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
-
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.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4