Linux:7 Images in Linux

From HandWiki
Jump to: navigation, search

Viewing Images

As with text, there are tools for both viewing and editing images. This chapter describes the various methods for viewing images; the editing of images is discussed in the next chapter). While you can view an image with an image editor, it is safer (and faster!) to view with a viewer when you do not intend to edit it.

18.1 Previewing Print Files

The DVI ("DeVice Independent"), PostScript, and PDF ("Portable Document Format") file formats can be generated by a number of applications. They are graphical image formats commonly used for printing; methods for previewing these files on the display screen are discussed in the following sections.

Previewing a DVI File

Use the xdvi tool to preview a DVI file in X. Give the name of the file to preview as an argument. xdvi will show how the document will look when printed, and let you view it at different magnifications.

  • To preview the file `gentle.dvi', type:

    $ xdvi gentle.dvi RET

To magnify the view of the document, left-click any of the buttons labelled with a percentage, such as 17%; they magnify the view by that percentage.

  • To magnify the view by 33%, left-click the button marked 33%.

The following table lists some of xdvi's commands.

COMMAND - DESCRIPTION

Q Exit xdvi and stop previewing the file.

N Advance to the next page.

P Move to previous page.

C-c Same as Q.

C-d Same as Q.

SPC Same as N.

C-l Redisplay the current page.

R Re-read the DVI file.

Previewing a PostScript File

To preview a PostScript or EPS image file in X, use ghostview. It takes a file name as an argument, and it previews the contents of the file in a window, starting with its first page.

  • To preview the file `/usr/doc/gs/examples/tiger.ps', type:

    $ ghostview /usr/doc/gs/examples/tiger.ps RET

Press Q to exit and press SPC to advance to the next page, if there is one.

NOTE: Some people prefer the gv tool as an alternate to ghostview; gv is used in much the same way, though it has a different interface.

Previewing a PDF File

Use xpdf to preview a PDF file. Give the name of the PDF file to preview as an argument.

  • To preview the PDF file `flyer.pdf', type:

    $ xpdf flyer.pdf RET

To exit xpdf, press Q; use the two magnifying-glass buttons to zoom the view closer in (+) or further out (-), and use the left and right arrow buttons to move to the previous and next pages, if any.

NOTE: You can also use gv to preview PDF files.

Viewing an Image in X

To view an image in X, use display, part of the ImageMagick suite of tools. It can recognize many image formats, including FlashPix, GIF/GIF87, Group 3 faxes, JPEG, PBM/PNM/PPM, PhotoCD, TGA, TIFF, TransFig, and XBM.

display takes as an argument the file name of the image to be viewed, and it displays the image in a new window.

  • To view the file `sailboat.jpeg', type:

    $ display sailboat.jpeg RET

This command displays the image file in a new window.

The mouse buttons have special meaning in display. Left-click on the image window to open the display command menu in a new window. Menu items let you change the image size and otherwise change or transform the image display. Choose Overview from the Help menu for an explanation of the various available display commands.

Middle-click on the image to open a new window with a magnified view of the image centered where you click. For example, middle-clicking on the sailboat image in the previous example will open a new window.

Finally, right-click on the image window for a pop-up menu containing a few of the most frequently-used commands; to choose one of these commands, drag the mouse pointer over the command. Commands in the pop-up menu include Quit, which exits display, and the Image Info, which displays information about the image file itself, including number of colors, image depth, and resolution.

The following table describes some of the keyboard commands that work when displaying an image in display.

COMMAND - DESCRIPTION

SPC Display the next image specified on the command line.

BKSP Display the previous image specified on the command line.

C-q Quit displaying the image and exit display.

C-s Write the image to a file.

< Halve the image size.

> Double the image size.

- Return the image to its original size.

/ Rotate image 90 degrees clockwise.

\ Rotate image 90 degrees counter-clockwise.

? Open a new window with information about the image, including resolution, color depth, format, and comments, if any.

h Toggle a horizontal mirror image.

v Toggle a vertical mirror image.

NOTE: display can also be used to view images on the World Wide Web.

Browsing Image Collections in X

The display tool offers a feature for browsing a collection of images--give `vid:' as the file argument, followed by the file names or pattern to match them in quotes. display makes thumbnails of the specified images, and displays them in a new window, which it calls a visual image directory.

  • To browse through the image files with a `.gif' extension in the `/usr/doc/imagemagick/examples' directory, type:

    $ display 'vid:/usr/doc/imagemagick/examples/*.gif' RET
  • To browse through all image files in the current directory, type:

    $ display 'vid:*' RET

In the preceding example, only those files with image formats supported by display are read and displayed.

NOTE: To open an image at its normal size, right-click the image and choose Load; the thumbnail will be replaced by its full-size image. To return to the thumbnail directory, right-click the image and choose Former.

Putting an Image in the Root Window

One way to put an image in the root window (the background behind all other windows) is to use display and give `root' as an argument to the `-window' option.

  • To put the image `tetra.jpeg' in the root window, type:

    $ display -window root tetra.jpeg RET

Browsing Images in a Console

Use zgv to view images in a virtual console (not in X). You can use zgv to browse through the filesystem and select images to view, or give as arguments the names of image files to view. It recognizes many image formats, including GIF, JPEG, PNG, PBM/PNM/PPM, TGA and PCX; one of its nicest features is that it can fill the entire screen with an image.

When you run zgv with no options, it displays image icons of any images in the current directory, listing any subdirectories as folder icons. You can also give the name of a directory as an argument in order to browse the images in that directory.


  • To browse the images in the current directory, type:

    $ zgv RET
  • To browse the images in the `/usr/share/gimp/scripts' directory, type:

    $ zgv /usr/share/gimp/scripts RET

Use the arrow keys to navigate through the file display; the red border around a image or directory icon indicates which image or subdirectory is selected. Type RET to view the selected image or to change to the selected directory.

You can manipulate the images you view in a number of ways--zoom the image magnification in and out, change the brightness and color, and even make automatic "slide shows" of images. The following table describes some of zgv's options.

OPTION - DESCRIPTION

-c - Toggle image centering. Images are centered on screen by default; specifying this option turns off centering.

-i - Ignore errors due to corrupted files, and display whatever portion of the file is displayable.

-l - Start zgv in slide-show mode, where it loops through all images specified as arguments, continuously, until you interrupt it.

-M - Toggle mouse support. Mouse support is off by default; this option turns it on.

-r integer - Reread and redisplay every image after every integer seconds. Useful for viewing webcam images or other image files that are continuously changing.

Viewing an Image in a Web Browser

If you have a graphical Web browser, such as mozilla, you can use it to view a graphic image. While viewing images in a browser doesn't offer much flexibility (you can't zoom in on a portion of the image, or get information about the image resolution and other details), if you simply want to quickly view an image file while you are in X, and you have a Web browser running, it can be a quick and easy way to do it.

To view an image file in a Web browser, specify a file: URL pointing to the file name of the image in the Location field of the browser.

  • To view the file `/usr/share/images/mondrian-15.jpeg', type:

    file:/usr/share/images/mondrian-15.jpeg RET

Notice that the given file: URL only has one preceding slash, pointing to the root directory, and not two, as in http://.

Browsing PhotoCD Archives

The xpcd tool is an X client for viewing and browsing collections of Kodak PhotoCD images. To browse the images on a Kodak PhotoCD, and then give the mount point as an argument to xpcd.

  • To browse the images on the PhotoCD disc mounted on `/cdrom', type:

    $ xpcd /cdrom RET

The preceding example will open two new windows--a small xpcd command bar window, and a larger window containing thumbnails of all PhotoCD images on the disc.

To open a copy of an image in a new window, left-click its thumbnail image. When you do, xpcd will open the image at the second-smallest PhotoCD resolution, 256x384; to view it at a another size, right-click the image and choose the size to view. Once the new window is drawn, you can right-click on this new image to save it as a JPEG, PPM, or TIFF format image.

To view an individual `.pcd' file with xpcd, give the name of the file as an argument.

  • To view the PhotoCD file `driveby-001.pcd', type:

    $ xpcd driveby-001.pcd RET

NOTE: You can also use display to view a `.pcd' PhotoCD image file.

Additional Image Viewers

The following table lists other tools for viewing images.

TOOL - DESCRIPTION

animate - Part of the ImageMagick suite; use animate to display an animated slide-show sequence of images in X. {@sf{Debian}}: `imagemagick' Image Magic

xwud - Displays files in the special X Window Dump file format, as created by xwd. {@sf{Debian}}: `xbase-clients' {@sf{WWW}}: http://www.xfree86.org/

showpicture - Views an image sent as an email attachment; requires xloadimage. {@sf{Debian}}: `metamail' {@sf{WWW}}: ftp://ftp.bellcore.com:/pub/nsb/mm2.7.tar.Z

xli - Basic image viewer for X. {@sf{Debian}}: `xli'

xloadimage - Nice graphics viewer for X that contains tools for viewing images in the root window. {@sf{Debian}}: `xloadimage'

aview - View graphics as "ASCII art." This tool can view any image format supported by the pbmplus utility suite, and has fluid zoom in/out and all the rendering options you'd expect from a world-class viewer.

Editing Images

Transforming Images

Many Linux tools can be used to transform or manipulate images in various ways. One very useful package for both transforming images and converting between image formats is the netpbm suite of utilities. Another is the ImageMagick suite of imaging tools, of which mogrify is particularly useful for performing fast command line image transforms; use it to change the size of, to rotate, or to reduce the colors in an image.

mogrify always takes the name of the file to work on as an argument, and it writes its changes to that file. Use a hyphen (`-') to specify the standard input, in which case mogrify writes its output to the standard output.

I'll use the image `phoenix.jpeg' in the examples that follow to give you an understanding of how to use mogrify:

Changing the Size of an Image

To resize an image with mogrify, use the `-geometry' option with the width and height values, in pixels, as an argument.

  • To resize `phoenix.jpeg' to 480x320 pixels, type:

    $ mogrify -geometry 480x320 phoenix.jpeg RET

Images scaled to a larger size will appear blocky or fuzzy.

When mogrify resizes an image, it maintains the image's aspect ratio, so that the ratio between the width and height stays the same. To force a conversion to a particular image size without necessarily preserving its aspect ratio, append the geometry with an exclamation point.

You can also specify the width or height by percentage. To decrease by a percentage, give the value followed by a percent sign (`%'). To increase by a percentage, give the value plus 100 followed by a percent sign. For example, to increase by 25 percent, give `125%'.

  • To increase the height of `phoenix.jpeg' by 25 percent and decrease its width by 50 percent, type:

    $ mogrify -geometry 125%x50% phoenix.jpeg RET

Rotating an Image

To rotate an image, use mogrify with the `-rotate' option followed by the number of degrees to rotate by. If the image width exceeds its height, follow this number with a `>', and if the height exceeds its width, follow it with a `<'. (Since both `<' and `>' are shell redirection operators, enclose this argument in quotes, omitting either if the image height and width are the same.)

  • To rotate `phoenix.jpeg', whose height exceeds its width, by 90 degrees, type:

    $ mogrify -rotate '90<' phoenix.jpeg RET

Adjusting the Colors of an Image

You can use mogrify to make a number of adjustments in the color of an image. To reduce the number of colors in an image, use the `-colors' option, followed by the number of colors to use.

  • To reduce the colors in `phoenix.jpeg' to two, type:

    $ mogrify -colors 2 phoenix.jpeg RET

Use the `-dither' option to reduce the colors with Floyd-Steinberg error diffusion, a popular algorithm for improving image quality during color reduction.

  • To reduce the colors in `phoenix.jpeg' to four and apply Floyd-Steinberg error diffusion, type:

    $ mogrify -colors 4 -dither phoenix.jpeg RET

Use the `-map' option with a second file name as an argument to read the color map, or the set of colors, from the second image and use them in the first image.

  • To change the colors in the file `rainbow.jpeg' to those used in the file `prism.jpeg', type:

    $ mogrify -map prism.jpeg rainbow.jpeg RET

Use the `-monochrome' option to make a color image black and white.

  • To make the color image `rainbow.jpeg' black and white, type:

    $ mogrify -monochrome rainbow.jpeg RET

If you have a PPM file, use ppmquant to quantize, or reduce to a specified quantity the colors in the image--see the ppmquant man page for details.

Because of differences in display hardware, the brightness of an image may vary from one computer system to another. For example, images created on a Macintosh usually appear darker on other systems. When you adjust the brightness of an image it is called gamma correction.

To adjust the brightness of an image, give the numeric level of correction to apply as an argument to the `-gamma' option. Most PC displays have a gamma value of 2.5, while Macintosh displays have a lower gamma value of 1.4.

  • To set the gamma correction of the image `rainbow.jpeg' to .8, type:

    $ mogrify -gamma .8 rainbow.jpeg RET

Annotating an Image

To annotate an image file with a comment, use mogrify with the `-comment' option, giving the comment in quotes as an argument to the option. This is useful for adding a copyright (or copyleft) statement to an image, or for annotating an image file with a URL.

  • To annotate the image file `phoenix.jpeg', type (all on one line):

    $ mogrify -comment "If you can read this, 
    you're too close!" phoenix.jpeg RET 

You won't see the annotation when you view the image; it is added to the image header in the file. You can, however, read image annotations with tools that display information about an image file, such as display or the GIMP. To read annotations in JPEG files, you can also use the rdjpgcom tool--it outputs any comments in the JPEG file whose file name is given as an argument.

  • To read any comments made in the image file `phoenix.jpeg', type:

    $ rdjpgcom phoenix.jpeg RET
    If you can read this, you're too close!
    $

Adding Borders to an Image

To draw a border around an image, use mogrify with the `-border' option followed by the width and height, in pixels, of the border to use.

  • To add a border two pixels wide and four pixels high to `phoenix.jpeg', type:

    $ mogrify -border 2x4 phoenix.jpeg RET

This transforms the original `phoenix.jpeg' to:

editing-images-borders-01

NOTE: The border is added to the outside of the existing image; the image is not cropped or reduced in size to add the border.

The `-frame' option works like `-border', but it adds a more decorative border to an image.

  • To add a decorative frame eight pixels wide and eight pixels high to `phoenix.jpeg', type:

    $ mogrify -frame 8x8 phoenix.jpeg RET

Making an Image Montage

To make a montage image of other images, use montage. It takes as arguments the names of the images to use followed by the name of the output file to write the montage image to.

The montage image is made by scaling all of the input images to fit the largest size possible up to 120x120 pixels, and tiling these images in rows of five and columns of four.

  • To create a montage from the files `owl.jpeg', `thrush.jpeg', and `warbler.jpeg' and write it to `endangered-birds.png', type:


$ montage owl.jpeg thrush.jpeg warbler.jpeg endangered-birds.png RET


NOTE: In this example, three JPEGs were read and output to a PNG file; to specify the format to use in the output, give the appropriate file extension in the output file name.

Combining Images

Use combine to combine two images into one new image--give the names of the two source image files and the new file to write to as arguments. Without any options, it makes a new image file by overlaying the smaller of the two images over the larger, starting in the top left corner; if both images are the same size, only the second image is visible.

  • To combine two images, `ashes.jpeg' and `phoenix.jpeg', into a new file `picture.jpeg', type:

    $ combine ashes.jpeg phoenix.jpeg picture.jpeg RET

You can specify the percentage to blend two images together with the `-blend' option. Give the amount to blend the second image into the first (as a percentage) as an argument to the option.

  • To combine the image files `phoenix.jpeg' and `ashes.jpeg' so that the blended image contains 70 percent of the second image, type:

    $ combine -blend 70 ashes.jpeg phoenix.jpeg picture.jpeg RET

This command combines the two images and writes a new image file, `picture.jpeg', whose contents contain 70 percent of the first image.

NOTE: Use `-blend 50' to blend the two source files equally.

Morphing Two Images Together

Morphing is a method of computer imaging for finding the difference between the shapes in two images; it's often used in special effects to transform aspects of two creatures, such as the faces of a human and some other animal.

You can use combine to get a morph-like effect by giving the difference argument to the `-compose' option. When specified with two input images and an output file, this command takes the difference between corresponding pixels in the two images; the effect is like a "morphed" image.

  • To make a morphed image of the files `ashes.jpeg' and `phoenix.jpeg', and write it to `picture.jpeg', type:
$ combine -compose difference ashes.jpeg phoenix.jpeg picture.jpeg RET

Converting Images between Formats

Use convert to convert the file format of an image. Give the name of the file to convert as the first argument, and the destination file as the second argument. When you convert a file, the original is not altered.

To specify the file type to convert to, use that file type's standard file extension in the file name of the converted file.

  • To convert the JPEG file `phoenix.jpeg' to a PNG image, type:

    $ convert phoenix.jpeg phoenix.png RET

This command converts the JPEG image `phoenix.jpeg' to PNG format and writes it to a new file, `phoenix.png'. The following table lists the file extensions to use and describes their format. (The convention is to give extensions in all lowercase letters.)

FILE EXTENSION IMAGE FORMAT

bmp - Microsoft Windows bitmap image.

cgm - Computer Graphics Metafile format.

cmyk - Raw cyan, magenta, yellow, and black bytes.

eps - Adobe Encapsulated PostScript.

fax - Group 3 fax format.

fig - TransFig image format.

fpx - FlashPix format.

gif - CompuServe Graphics Interchange Format, version GIF89a (usually pronounced "giff," rhyming with "biff").

gray - Raw gray bytes.

jpeg and jpg - Joint Photographic Experts Group JFIF format (usually pronounced "jay-peg").

pbm - Black and white portable bitmap format.

pcd - Kodak PhotoCD format, 512x768 pixels maximum resolution.

pcl - Page Control Language format.

pcx - ZSoft IBM PC Paintbrush format.

pdf - Adobe Portable Document Format.

pict - Apple Macintosh QuickDraw format.

png - Portable Network Graphics format (usually pronounced "ping").

pnm - Portable "anymap" format.

ppm - Color portable pixmap format.

ps - Adobe PostScript format.

rgb - Raw red, green, and blue bytes.

tga - TrueVision Targa image format.

tiff and tif - Tagged Image File Format (usually pronounced "tiff").

xbm - X Window System bitmap format.

xpm - Color X Window System pixmap format.

xwd - Color X Window System window "dump" file format.

When converting a file to JPEG format, be sure to use the `-interlace NONE' option to make sure the resultant JPEG image is non-interlaced--unless, of course, you want an interlaced image; an interlaced image is drawn in multiple passes, and is often used on the Web where a reader may view the low-resolution image consisting of early passes before the entire image is downloaded. A non-interlaced image is drawn in one single pass.

For example, use convert to convert a PNM file to non-interlaced JPEG, while sharpening it, adding a border, and adding a copyright statement.

  • To convert the PNM file `pike.pnm' to non-interlaced JPEG while sharpening the image by 50 percent and adding both a 2x2 border and a copyright comment, type:

    $ convert -interlace NONE -sharpen 50 -border 2x2
    -comment 'copyright 1999 MS' pike.pnm pike.jpeg RET

This command writes its output to a file `pike.jpeg'. Notice that the options `-border' and `-comment' were previously described for the `mogrify' tool. Some ImageMagick tools share common options, which is useful if you are making multiple changes to an image file at once; only one tool is needed for the job.

NOTE: Some image formats are "lossy," in that some image information is lost when you convert to it. For example, the JPEG format is a lossy format that is usually used for photographic images. If you convert a file from its source PNM format to JPEG and then back to PNM, the resultant PNM will not be identical to the original source PNM.

To convert image files interactively, use the GIMP to open the image, and then choose `Save as' from the File menu, and select the file type to use.


Editing Images with the GIMP

The GIMP (GNU Image Manipulation Program) is an all-encompassing image-editing and manipulation program that lets you paint, draw, create, and edit images in complex ways. Using gimp you can also convert image files, retouch and edit photographic images, and browse collections of images.

The GIMP comes with hundreds of tools, filters, fonts, and other goodies installed. Here is a partial list of its features:

  • Contains a full suite of painting tools, including Brush, Pencil, Airbrush, and Clone.
  • Supports custom brushes and patterns.
  • Includes a full suite of image selection, transformation, and manipulation tools, including a gradient editor, color blending, and special effects.
  • Includes animation support.
  • Permits the use of layers and channels.
  • Allows for large images, with their size being limited only by available disk space.
  • Provides high-quality anti-aliasing.
  • Offers full alpha-channel support.
  • Supports command scripting.
  • Permits multiple undo and redo, limited only by available disk space.
  • Allows multiple images to be open simultaneously.
  • Supports all popular file formats, including GIF, JPEG, PNG, XPM, TIFF, TGA, MPEG, PS, PDF, PCX, and BMP.
  • Allows the easy addition of more than 100 plug-ins for new file formats and new effect filters.

The GIMP runs under X and is started by running gimp or choosing it from your window manager's menu.

NOTE: To learn the basics of the GIMP, consult The GIMP User's Manual and the other documentation and resources on the Web at http://www.gimp.org/. You can also install the manual on your system; it comes in the Debian `gimp-manual' package.

Interactive Image Editors and Tools

There are all kinds of image-editing software applications available for Linux--and there are as many way to make and edit an image as there are tools to do it with.

The following table lists some other popular tools and applications for making and editing images--including CAD engineering software--that you may want to explore. It is not exhaustive.


TOOL DESCRIPTION

bitmap Use to edit bitmap files, which are used for icons and tile patterns in the X Window System. {@sf{Debian}}: `xbase-clients'

drgeo is a program for drawing interactive geometric figures. {@sf{Debian}}: `drgeo'

dia - Use dia to draw simple charts and diagrams. It saves files in its own format, but you can export files to EPS; if you plan on editing a diagram file again, however, be sure you keep the `.dia' file since, as of this writing, dia cannot import EPS files. {@sf{Debian}}: `dia'

electric - Use for designing images of electronic circuitry.

freedraft - FREEdraft is a 2-D mechanical CAD tool for precision drawing and sketching.

gnuplot - is a robust, non-interactive function-plotting tool. Given a data file and a formula, gnuplot can make charts and graphs. {@sf{Debian}}: `gnuplot'

ivtools - The ivtools suite of software includes idraw, a vector graphics editor. {@sf{Debian}}: `ivtools-bin' {@sf{WWW}}: http://www.vectaport.com/

kali - Use for drawing patterns and tilings, including frieze patterns and infinite or recursive tiles in the spirit of M.C. Escher. {@sf{Debian}}: `kali'

moonlight - The Moonlight Creator is an X client for modeling, illuminating, and rendering 3-D scenes. {@sf{Debian}}: `moonlight' http://www.cybersociety.com/moonlight/

sced is a tool for creating 3-D scenes. {@sf{Debian}}: `sced'

xfig - Use the venerable xfig application for drawing figures--complex graphs, floor plans, maps, flow charts, and so forth. It saves files in its own format (giving them a `.fig' extension by default); the usual thing to do is export to EPS. {@sf{Debian}}: `xfig'</code

xmorph - is a tool to morph (sometimes called "warp") two images together, making a new image in the process. Images must be in TrueVision Targa file format, with the same size, shape, and number of pixels in each file. xpaint

xpaint, a simple "paint" tool that predates the GIMP, contains all of the basic features that you would expect from a paint program.