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
-
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 is 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.
-
Star Labs Reveals a New Surface-Like Linux Tablet
If you've ever wanted a tablet that rivals the MS Surface, you're in luck as Star Labs has created such a device.