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
-
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.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.