Running Linux, 4th Ed.Running Linux, 4th Ed.Search this book

9.4. Graphics

Many people are fascinated by computer graphics. Computers are being used to create photorealistic images of surreal scenes or fractally generated images of mountain ridges with lakes and valleys; to change images by bending, polishing, and aging them; or to make any other manipulations.

Linux does not need to step shyly aside when it comes to graphics. It can do just about anything that other computing environments can do, and in some areas, such as dealing with many graphics files at the same time, it even excels. The X Window System, described in the next chapter, forms a very good base for bitmapped graphics. There is now also hardware support for 3D graphics conforming to the OpenGL standard.

However, working with graphics on Linux is sometimes different from what you might be used to on other operating systems; the Unix model of small, interoperating tools is still alive and well here, too. This philosophy is illustrated most clearly with the ImageMagick suite of graphics manipulation programs, which we will describe here. ImageMagick is a collection of tools that operate on graphics files and are started from the command line or from shell scripts. Imagine that you have 2,000 files of one file format that you want to reduce to 256 colors, slant, and convert to another file format. On Linux, this requires only a few lines in a shell script. Now imagine doing this on Windows: click the File menu, click the Open menu entry, select a file, select an operation, specify the parameters for the operation in a dialog, click OK, choose Save from the menu, select a filename, and then click OK. Now repeat for next 1999 files. Can you say RSI?[36]

[36]Repetitive Strain Injury — a malady that comes from typing too much. The better known carpal tunnel syndrome is one form of RSI.

Graphics can not only be drawn, but also programmed. Certain tools, such as the ray-tracer POVRAY presented in this chapter, enable you to specify the graphics to be generated by a suite of commands, often in a full-blown graphics programming language. While this is perhaps more difficult than drawing the desired graphics with a mouse, it is also infinitely more flexible once you have mastered how to use it.

Now, not everybody wants to work with graphics from the command line. Part of the appeal of working with computer graphics is the immediate feedback you get when working with graphics programs, and Linux can provide this, too. The GIMP, which we will be covering shortly, is a fascinating package for interactive image manipulation that is superior to a lot of commercial graphics manipulation software on other systems.

Many more graphics programs are available for Linux, including 3D modelers and video players. Scouring some Linux archives will reveal a wealth of good software. Most of these programs require the X Window System, which we'll show you how to install and configure in later chapters.

9.4.1. ImageMagick

ImageMagick is a suite of programs that convert and manipulate graphics files from the command line. It is also well suited for batch conversions — that is, converting many files at once. In addition, ImageMagick includes PerlMagick, a binding that lets you invoke the ImageMagick utilities from the Perl programming language. It comes with most Linux distributions; if yours does not have it, you can get it from ftp://ftp.x.org/contrib/applications/ImageMagick.

ImageMagick consists of a library and a number of utilities:

display
display is a graphical frontend to many of the functions provided by ImageMagick. It is very useful if you want to experiment with the effects of a function before you go and batch-convert hundreds of images with these effects. display can load many different graphics file formats and display them on the X Window System. You can start display and pass it one or more filenames on the command line, or load a file from the menu provided. If you have started display and do not see a menu, click the image displayed with the left mouse button. display features a huge number of command-line switches. See the manual page for details.

import
import is a small program that captures either one window or the whole server into a file; that is, it allows you to make screenshots. For example, to make a screenshot of your whole screen and save it as myscreen.xpm in the graphics file format XPM, you would execute:

tigger$ import -window root myscreen.xpm

When you hear the first beep, the screen capture begins, and when the second one sounds, capturing is finished and the image data is in the process of being saved to the file you specified.

If you want to capture the contents of only one window, the easiest way is to start import without the -window option:

tigger$ import mywindow.xpm

The cursor then turns into a crossbar, which prompts you to click any window. This window's contents are then captured and saved to the specified file.

Like all ImageMagick programs, import has many command-line options; check the image(1) manual page.

montage
montage is a very useful little tool with a functionality rarely found elsewhere. It takes a number of images and puts them together as one large image in a tiled manner. There are lots of options for changing how the images are exactly tiled and put together.

In order to tile all your JPEG images in the current directory and create the image all.jpg out of it, you could call:

tigger$ montage *.jpg all.jpg

By default, there will be a label with the filename below each image. You can avoid this by entering:

tigger$ montage +frame *.jpg all.jpg
convert
In a way, convert is the heart of the ImageMagick suite. It can convert between an amazing number of graphics formats. For example, let's assume that you want to port a GUI program from Windows to Linux. You have a large number of toolbar icons in Windows BMP format and want to convert those to XPM. You could do this with:

for i in *.bmp
do
convert $i xpm:'basename $i .bmp'.xpm
done

convert will happily chug through all the images and convert them. If you want to do other things to the images, just add the necessary switches — e.g., -despeckle for removing speckles from the images.

mogrify
mogrify is like convert, but it overwrites the original images and is meant more for applying filter functions than for converting file formats (even though you can easily change the file format by using the switch -format).

identify
identify outputs information about the images passed on the command line. For example:

tigger$ identify tux.gif
tux.gif 257x303+0+0 DirectClass 10968b GIF 1s

This tells you, among other things, that tux.gif is a GIF file 257 pixels wide and 303 pixels high. If this is not enough information for you, try the -verbose switch!

combine
As its name indicates, combine combines several images into one. You can, for example, put color-separated images together or put a logo on top of a larger image.

xtp
Like the ftp program, xtp downloads files from a remote site or uploads them to a remote site. But unlike ftp, it does not need interactive input (in order to get this with ftp, you would have to edit its configuration file). You can productively combine xtp with the other tools from the ImageMagick suite to automatically manipulate images on remote servers.

To sum up, the programs from the ImageMagick suite are an extremely versatile means to manipulate graphics files. However, it takes some time to familiarize yourself with all the command-line options in order to know what is available.

9.4.2. The GIMP

The GIMP (which expands to either GNU Image Manipulation Program or General Image Manipulation Program and is often written with the definite article) specializes in image manipulation like the ImageMagick package described in the last section. But while ImageMagick's strength is batch processing, GIMP does everything via its GUI. The GIMP is often cited as one of the most impressive and successful products for Linux (and other Unix versions). People who use image manipulation programs professionally have said that while GIMP can feel a little bit awkward to use, it is functionally comparable to its commercial competitor from the Windows and Macintosh world, Adobe Photoshop. Some people even have called it "the free alternative to Photoshop."

GIMP draws its power from an amazing number of plug-ins that are available for it. Thus, in order to be able to use a newly written image manipulation filter or file format import filter, you only need to install the plug-in and restart GIMP, and you are ready to go.

In addition, GIMP uses a clever tiling mechanism that allows you to load arbitrarily large images into it; GIMP will keep in memory only the part that is currently being used or visible. And if these features are not enough, GIMP also has its own scripting language called script-fu, and can be scripted with Perl as well.

GIMP comes with most Linux distributions on the market today and can be run simply by entering the command gimp in a shell after starting X. If your distribution does not have GIMP, you can get the source code from www.gimp.org. If you plan to do more work with GIMP, you should check this web site anyway because it also contains the documentation and sample images that show off what you can do with it. A screenshot showing the toolbar, dialog boxes, and a work area is shown in Figure 9-5.

Figure 9-5

Figure 9-5. The Gimp

9.4.3. POVRAY

While ImageMagick and GIMP are mainly for manipulating existing images, POVRAY (the first three letters stand for Persistence Of Vision) is a program for automatically creating new images. It is a so-called ray-tracer; a program that computes a scene from some information about which objects should be in the scene, what their physical and optical properties are, where the light is coming from, and where the spectator is standing. You describe this to POVRAY in a programming language of its own.

This brief description should already indicate that using POVRAY is not something you learn in one afternoon. If you are interested in this program, you can download it from http://www.povray.org, which is also the first source for all kinds of POVRAY resources like sample files, documentation, and so on. Since we cannot give you even a crash course into using POVRAY here, we'll refrain from attempting this futile task.

If you are interested in POVRAY, though, but find the task of describing your scenes in a programming language daunting, it might interest you to know that certain graphic modeling programs can be used with ray-tracers like POVRAY, so you do not have to create the 3D models completely by hand. One of these programs is Blender, about which you can find more information at http://www.blender3d.com.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.