F.3 ImageMagick

ImageMagick is a free software project (with an Apache-like license) that supports about 100 image formats and format conversions. ImageMagick is also capable of doing a number of sophisticated things to images. For more details, check out its home page (http://www.imagemagick.org/).

Calling ImageMagick

ImageMagick is normally called from a terminal or command line, but its functionalities can also be accessed from other programs. When using it to process many files, you’ll find the filename globbing facilities of GNU/Linux extremely useful. For example, the command

convert *.jpg animation.gif

converts all the files in the current folder with a .jpg suffix to a single GIF animation called animation.gif. The asterisk is a wildcard, which matches any character or string. The Windows command language does not support globbing, but ImageMagick adds support, so you can use the same commands in Windows.

ImageMagick contains 10 different programs, the most useful being convert, which we used in the previous example. Among the myriads of possible usages, we describe just a few to give you a taste of this powerful application. These examples use GNU/Linux shell notation, but Windows users can refer to the detailed page http://www.imagemagick.org/Usage/windows/. But this first example is given in both notations.

Suppose we have a folder with a lot of photographs—called img001, img002, and so on— in TIFF format, and we want to convert all of them to the PNG format. The input files do not have a file extension, so we have to tell the convert command what their format is. We do this with the prefix tiff:. With GNU/Linux, here is how we write it:

for i in img*
do convert tiff:$i $i.png
done

With Windows and the DOS shell, we write it as

FOR %a in (img*) DO ^
  convert tiff:%a %a.jpg

The caret is used to continue the command on the next line.

Another solution is to use the mogrify command, which does not check whether the target files already exist and happily overwrites files when asked. The following command would work in both GNU/Linux and Windows:

mogrify -format png tiff:*

But avoiding this command is best because accidentally erasing files is very easy.

Building Thumbnails

One of ImageMagick’s main purposes is to generate thumbnails. ImageMagick makes generating uniform-looking thumbnails for a whole set of images easy. You can use thumbnails on a web page, for example, as links to the actual images. Because thumbnails are normally small images, say 200 × 200 at most, generate them in GIF format, which compresses well. At this size, the limitation to 256 colors is not a problem.

The following example generates the thumbnail image thumbnail.gif from the source image image.jpg:

convert -define jpeg:size=500x180
  image.jpg -auto-orient
  -thumbnail 250x90 -unsharp 0x.5
  thumbnail.gif

The command is written here on four successive lines, but, in fact, you type it on one line only.

The -define option is not necessary, but it may accelerate the process if the source image is very large. The JPEG library must enlarge the (compressed) image when it loads, and this option sets the approximate size that the image is enlarged to. The -auto-orient option uses the EXIF information provided by the camera and rotates the image if necessary. The -thumbnail option sets the final dimensions of the thumbnail and can be used to discard any useless information from the image, such as comments. The aspect ratio is maintained: The resulting thumbnail is 90 pixels high, but its width is 250 pixels or less. Finally, the -unsharp option is used to sharpen the image because resizing always results in a slight blur.

To generate thumbnails for several images at the same time, you could use the mogrify command, but calling convert in a loop is safer. One challenge of generating thumbnails from source images is getting thumbnails with names like img0567.jpg.gif. mogrify can solve the problem with its -format option. If you need to use convert, with GNU/Linux you can use the shell capabilities for discarding a file extension:

for i in *
do convert $i ${i%jpg}gif
done

The notation ${i%jpg}gif discards the jpg extension from the filename and replaces it with the gif extension, which also tells ImageMagick which conversion to perform.

Many other additions could be made to the simple example just given. Review http://www.imagemagick.org/Usage/thumbnails/.

Labels and Transformations

ImageMagick can be used to add labels to images:

convert image.jpg -background Khaki
  label:'Label' -gravity Center
  -append labeled-image.jpg

In this example, a fixed label with a khaki background is added to the bottom center of the image. If you want to label many images, use the following example (with the GNU/Linux shell):

for i in *
do convert $i -background Khaki
   label:"${i%.jpg}" -gravity
   Center -append labeled-$i
done

Here, the images are labeled with their name minus the extension, and the labeled image name is prefixed with labeled-.

Use ImageMagick to perform many of the transformations that GIMP does but on lots of images consecutively. For example, decide which transformations you want to perform, as well as the parameter values. Then you can use the convert command with many options and parameters to reproduce the GIMP transformation over and over. Test it on the first image. Then embed this command in a loop, start it, and have a beverage while the computer works.

Once again, the ImageMagick website is an invaluable source of really sophisticated examples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset