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.

The Author

Wolfgang Dautermann works as a system administrator at the FH Joanneum University of Applied Sciences and is one of the organizers of Graz Linux Days.

Rene Schickbauer works as a system administrator at Magna Powertrain.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Tweak Images with Converseen
  • Image Wizardry

    The free ImageMagick graphics toolbox brings the feature set of a full-blown image processor to the command line.

  • ImageMagick

    GIMP isn’t the only option for photo manipulation. ImageMagick, a collection of command-line programs for image processing, can help you process multiple images in one go.

  • Tutorial – ImageMagick

    ImageMagick can do more than just edit existing images. The free software can even be scripted to create simple drawings.

  • ExactImage

    Automate image processing with this quick and easy-to-use photo tool suite.

comments powered by Disqus
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.

Learn More

News