Image editing with command-line tools
On the Line

© Lead Image © Iryna Denysova, 123RF.com
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
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.