Image editing with command-line tools
Your Own Programs
ImageMagick is more than just a command-line tool. Using the system
call, you can assemble a command string and execute it in programming languages such as C, PHP, or Perl. ImageMagick provides extensive APIs for many programming languages [5].
As a first example, in a PHP program the variable $_SERVER['HTTP USER_AGENT']
tells you if a user accesses a website with a PC browser or web browser on a mobile device. Ideally, you will want to serve the graphics to this user in a scaled-down form on the fly to save data volume and increase compatibility. You will need php5-imagick
PHP extension for this; Listing 7 shows how it works. Instead of scaling to a fixed width of 200 pixels, as in the sample program, you could let visitors choose in advance the extent to which the website should constrain the graphics.
Listing 7
PHP Example
<?php // Output PNG image header("Content-type: image/png"); // Import example graphic $thumb=new Imagick("example.png"); // sclae to 200px width $thumb->thumbnailImage(200,0); // Output image echo $thumb; ?>
The short Perl example in Listing 8 uses ImageMagick or the Image::Magick Perl module to input a color PNG image, convert it to monochrome, and save the results in a GIF file.
Listing 8
Perl Example
#!/usr/bin/perl use strict; use warnings; use Image::Magick; my $image = Image::Magick->new; $image->Read('color.png'); $image->Quantize(colorspace=>'gray'); $image->Write(filename=>'black-and-white.gif');
Finally, the C programming example in Listing 9 uses the GraphicsMagick fork of ImageMagick (see the "GraphicsMagick" box) to recreate a convert
(i.e., gm convert
) call. ImageMagick and GraphicsMagick both provide a low-level interface and a simple higher level C API called MagickWand.
Listing 9
GraphicsMagick
/* Converts an input image into a output image in a different image format. Call: myconvert input image output image */ #include <wand/magick_wand.h> int main(int argc, char **argv) { /* MagickWand-Handle */ MagickWand *mw; /* Initializing GraphicsMagick */ InitializeMagick(*argv); mw=NewMagickWand(); /* Read image */ MagickReadImage(mw, argv[1]); /* Write image in new format */ MagickWriteImage(mw, argv[2]); /* Uninitializing GraphicsMagick */ DestroyMagickWand(mw); DestroyMagick(); }
GraphicsMagick
GraphicsMagick [6] is a fork of ImageMagick from 2002 with capabilities very similar to the original. However, unlike ImageMagick, which comprises several individual commands such as convert
, mogrify
, and display
, GraphicsMagick only has a single generic command, gm
, that you must supplement with the desired operation (e.g., gm convert
or gm mogrify
). Table 1 describes the options for both tools.
Table 1
Programs and Features
ImageMagick | GraphicsMagick | Feature |
---|---|---|
animate |
gm animate |
Display a series of images |
– |
gm batch |
Execute several commands as a script |
– |
gm benchmark |
Benchmark a command |
compare |
gm compare |
Compare images |
composite |
gm composite |
Superimpose images |
conjure |
gm conjure |
Run Magick Scripting Language XML script |
convert |
gm convert |
Convert images |
display |
gm display |
Display image |
– |
gm help |
Display help |
identify |
gm identify |
Display image information |
import |
gm import |
Create screenshot |
mogrify |
gm mogrify |
Modify image |
montage |
gm montage |
Combine images |
stream |
– |
Read image section |
– |
gm time |
Measure a command's execution time |
– |
gm version |
Display version |
The example in Listing 9 only converts between different graphic formats (e.g., from GIF to PNG) and doesn't take advantage of the countless options of the original Image- and GraphicsMagick programs. To compile the source code, use the command:
$ gcc myconvert.c -omyconvert $(GraphicsMagickWand-config \ --cppflags --ldflags --libs)
The options in the parentheses ensure that the GCC compiler uses the correct flags and libraries.
Other Programs
Besides the two top dogs, with their wide range of functions, some additional command-line programs exist with very specific skills. The most important are:
- ExifTool [7] – modifies Exif metadata.
- OptiPNG [8] and PNG Crush [9] – optimize and reduce PNGs.
- jpegtran [10] – performs lossless modifications on JPEGs.
- Steghide [11] and OutGuess [12] – hide information in images using steganography.
The ExifTool program outputs the Exif metadata from images and modifies them as required. The metadata includes, among other things, the type of camera used, the capture date, and the GPS coordinates and serial number of the camera under certain circumstances. With exiftool -list
you can view the tool's capabilities; exiftool image.jpg
outputs the Exif metadata contained in the file. The
exiftool -all= image.jpg
command deletes all unwanted metadata, which could prove to be useful if you want to post pictures online. Alternatively, you can add more metadata. For example, the
exiftool -comment="<Text>" image.jpg
command writes a comment in the picture's Exif metadata.
The optipng image.png
call optimizes PNGs (e.g., by reducing the color depth or providing higher compression). The tool reduced the file size of an image by almost 70 percent in our lab. You can modify JPEG images using jpegtran
. The commands
jpegtran -optimize a.jpg >b.jpg jpegtran -rotate 90 a.jpg >b.jpg
optimize an image and rotate it by 90 degrees, respectively.
Finally, the tools Steghide and OutGuess can serve to hide information in images using steganographic techniques. Steganography methods [13] hide data in files. Images with a high color depth are well suited for this process. Minimal changes to the color depth, for example, are often sufficient, and the human eye cannot see the difference. The modified bits then contain the hidden information.
Conclusions
Command-line programs leave their GUI cousins far behind when it comes to modifying one or multiple images. Thanks to the tools' corresponding APIs, you can even expand your own programs with ImageMagick's graphics capabilities.
Infos
- ImageMagick: http://www.imagemagick.org
- "GNU Parallel" by Tim Schürmann, Ubuntu User, issue 18, 2013, pg. 20, http://www.ubuntu-user.com/Magazine/Archive/2013/18/Accelerated-command-processing-with-GNU-Parallel
- Convert drawing functions: http://www.imagemagick.org/Usage/canvas/
- MSL: http://www.imagemagick.org/script/conjure.php
- ImageMagick APIs: http://www.imagemagick.org/script/api.php
- GraphicsMagick: http://www.graphicsmagick.org
- ExifTool: http://owl.phy.queensu.ca/~phil/exiftool/
- OptiPNG: http://optipng.sourceforge.net
- PNG Crush: http://pmt.sourceforge.net/pngcrush/
- jpegtran: http://www.libjpeg-turbo.org/
- Steghide: http://steghide.sourceforge.net
- OutGuess: http://www.outguess.org
- "Steganography" by Kurt Seifried, Linux Pro Magazine, issue 112, March 2010, pg. 20
« Previous 1 2 3
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