Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


Living Linux

Scanning Images With SANE

07/18/2000

SANE, "Scanner Access Now Easy," is the de facto Linux scanner interface, which you use to scan an image from an image scanning device -- such as a flatbed scanner -- and save it to an image file.

The SANE tools are available in the Debian sane package or on the Web at http://www.mostang.com/sane/.

As the acronym implies, getting a scanner to work on a Linux system hasn't always been smooth going. The SANE interface is completely open, and its developers are making sure that it is generalized enough to be implementable on any hardware or operating system.

SANE works with a wide array of scanning hardware, but you'll want to make sure the scanning hardware you want to use is compatible by checking the Hardware HOWTO and SANE's list of supported scanners.

Once you have SANE installed and going, you can scan images with applications like the GIMP image editor; do this to scan images with a self-explanatory, point-and-click interface. The rest of this column focuses on a command-line scanimage tool, which comes with the SANE package.

Listing your scanner devices

You'll need to know the names of your scanner devices before you can use them. Get them by using scanimage with the --list-devices option:

scanimage --list-devices
device 'umax:/dev/sgb' is a 
  UMAX   Astra 1220S   flatbed scanner

In this example, there's one scanning device on this system, a UMAX scanner which can be used to scan by using its device name, 'umax:/dev/sgb,' as an argument to the -d option.

To list the available resolutions and options that are supported by a particular device, use the --help option along with the -d option followed by its device name.

For example, to list available options supported by the device listed above, you'd type:

scanimage --help -d 'umax:/dev/sgb'

For all scanimage commands, to specify the scanner device you want to use, include the -d option with the device name.

Making sure your scanner works

To test a scanner and make sure it works, use the --test option, which performs diagnostic tests to make sure that the scanner is being read from properly.

For example, to test the scanner device from the above example, you'd type:

scanimage --test -d 'umax:/dev/sgb'

Scanning an image

To scan an image with scanimage, most scanning devices let you specify the x and y values, in pixels, for the image size to scan as arguments to the -x and -y options from the top-left corner of the scanner bed. Also give an argument to the --resolution option to specify the scan resolution. Scanner resolutions are given in dpi, or dots per inch.

Common resolution values include 72, 120, 300 and 600 dpi; 72 dpi is the most popular resolution for use on the Web or for just viewing on the screen, and 204 dpi is often used for images that you want to send on a fax machine.

The output is sent to standard output -- so to scan an image to a file, redirect the standard output to a file.

scanimage outputs images in the PNM ("portable anymap") formats, so you'll want to make sure that you have the netpbm package installed as well; this is a useful collection of tools for converting and manipulating these formats, and most Linux systems come with this package installed by default. The PNM formats output by scanimage are as follows:

Use the --mode option to specify the format of the output. Follow it with one of the following arguments: "color" for color PPM, "lineart" for black and white PBM, or "gray" for PGM grayscale. Each scanner has a default mode; for most color scanners, the default mode will be "color."

For example, to make a 72 dpi scan of a color image 200 pixels wide and 100 pixels tall, scanned from the device in the above examples and output to a file called scan.ppm, type (on one line):

scanimage -d umax:/dev/sgb 
  --resolution 72 -x 200 -y 100 > scan.ppm

To make a 300 dpi scan of a black and white image 180 pixels wide and 225 pixels tall, scanned from the device in the above examples and output to a file called scan.pbm, type (on one line):

scanimage -d umax:/dev/sgb --resolution 300 
 --mode lineart -x 180 -y 225 > scan.pbm

From here, you can open the file in an image editor such as the GIMP, do command-line image transformations, or use the netpbm tools to manipulate the scanned image.

Next week: about one of the philosophies that Linux is built on.

Michael Stutz was one of the first reporters to cover Linux and the free software movement in the mainstream press.


Read more Living Linux columns.

Copyright © 2009 O'Reilly Media, Inc.