Home / Alt manpages / sgitopnm(1)

  • sgitopnm(1)
  • User command
  • linux

Convert SGI Images to PNM with sgitopnm

You will convert an SGI image into a PGM greyscale image or a PPM colour image, optionally extract one channel, and check the result without touching the source file. The examples use Netpbm 11.5.2, installed here as package version 2:11.05.02-1.1build1. Allow about ten minutes if you already have an SGI file and a writable working directory.

You need the netpbm package and a readable SGI image. These are ordinary user operations. Do not use sudo for image conversion unless the input directory or output directory is deliberately restricted by its owner. The converter reads the source and writes the PNM result; it does not alter the SGI file.

1. Check the installed command

Confirm which executable will run and record the package version:

$ command -v sgitopnm
/usr/bin/sgitopnm
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ sgitopnm --version
sgitopnm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
sgitopnm: Built from source dated 2024-03-31 09:09:47

The version output also prints build details and tells you to use the manual for help. That is expected. The useful check is that the command identifies the Netpbm library version you intended to use.

Checkpoint

If command -v finds nothing, stop here and install or repair Netpbm through your normal package-management process. Do not copy an executable into a system directory as a quick fix.

2. Convert an SGI file to PGM or PPM

Pass the SGI path as the optional input argument and redirect standard output to a new file:

$ sgitopnm /path/to/input.sgi > converted.pnm
sgitopnm: writing PGM image

A two-dimensional SGI file with one or two channels produces PGM. An SGI file with three or more channels produces PPM, using the first three channels for the colour image. The diagnostic names the chosen output type. If the input is a three-channel file, expect writing PPM image instead.

Check the output rather than trusting the filename:

$ file converted.pnm
converted.pnm: Netpbm image data, size = 1920 x 1080, rawbits, greymap
$ head -c 2 converted.pnm
P5

PGM uses the P5 magic number in this binary output. A colour result should report a pixmap from file and begin with P6. Exact wording varies between file releases, but the dimensions and magic number should agree with the image you supplied.

3. Keep failed conversions from replacing good output

Shell redirection truncates its destination before sgitopnm starts. For an output name that might already contain a useful image, write to a temporary file in the same directory and replace the destination only after verification:

$ sgitopnm /path/to/input.sgi > converted.pnm.new
$ file converted.pnm.new
converted.pnm.new: Netpbm image data, size = 1920 x 1080, rawbits, greymap
$ mv converted.pnm.new converted.pnm

The mv command changes the directory entry and can replace an existing file. Treat it as the irreversible step in this example: verify the new file first, and choose a different destination if you need to retain the old result. If conversion fails, leave the old output alone and remove only the incomplete .new file after checking what was created.

4. Inspect the SGI header without opening the image

Use -verbose when you need the raster dimensions, channel count, compression and image name:

$ sgitopnm -verbose /path/to/input.sgi > converted.pnm
sgitopnm: raster size 1920x1080, 3 channels
sgitopnm: compression: 0x01 = RLE
sgitopnm: Image name: 'no name'
sgitopnm: writing PPM image

These details explain what the converter selected. 0x01 = RLE means the SGI file uses run-length encoding. A different compression line is not automatically an error. The image name is metadata from the SGI header and may be no name.

Checkpoint

Compare the reported dimensions and channel count with the source application's records before using the PNM in a batch job. A successful conversion does not prove that the picture is the one you intended.

5. Extract one channel as PGM

Use -channel when you need a single component rather than the normal PGM or PPM result:

$ sgitopnm -channel=0 /path/to/input.sgi > channel-0.pgm
sgitopnm: writing PGM image
$ file channel-0.pgm
channel-0.pgm: Netpbm image data, size = 1920 x 1080, rawbits, greymap

On the installed Netpbm 11.5.2 build, channel numbers are zero-based: a three-channel image accepts 0, 1 and 2. Each selected channel is written as PGM. A value outside the image's channel range fails rather than silently producing an empty or guessed image:

$ sgitopnm -channel=3 /path/to/three-channel.sgi > channel-3.pgm
sgitopnm: channel out of range - only 3 channels in image

Keep the error on screen or redirect it separately when scripting. Do not infer a valid channel from a filename. For two-channel SGI images, the documented format is greyscale plus transparency; if you need both components in a PAM image, extract the channels and combine them with pamstack after checking their order.

6. Read an SGI image from standard input

If the input argument is absent, sgitopnm reads standard input. This lets you place it after a producer or feed an archive member without making a second permanent copy:

$ cat /path/to/input.sgi | sgitopnm > converted.pnm
sgitopnm: writing PGM image

Netpbm before version 10.67 required a seekable input file, but the installed 11.5.2 program accepts this pipe. If a much older host rejects a pipe, save the SGI data to a regular file and pass that path instead. Keep binary data in the pipeline; do not send it through text-processing commands.

7. Diagnose the common failures

An error opening the input usually means the path, permissions or file format needs checking:

$ test -r /path/to/input.sgi && echo readable
$ file /path/to/input.sgi

If file does not identify SGI data, stop before trying channel options. If the result has the wrong dimensions, rerun -verbose and compare the header with the source. If a channel request fails, count from zero and stay below the reported channel count. A non-zero exit status means the conversion did not complete successfully; keep the original SGI file for another attempt.

Done means

  • The installed Netpbm version and executable were confirmed.
  • The SGI source remains unchanged.
  • The output's PGM or PPM type and dimensions were checked.
  • Channel extraction used a valid zero-based channel number.
  • A pipe was used only on a Netpbm version that supports non-seekable input.
  • Any existing output was protected until the replacement had been verified.