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
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.