Home / Alt manpages / pnmtosgi(1)

  • pnmtosgi(1)
  • User command
  • linux

Convert PNM Images to SGI Safely with pnmtosgi

You will convert a PBM, PGM or PPM image into an SGI image file, choose whether the result is compressed, and check that the output can be read back. The examples use Netpbm 11.5.2 from package version netpbm 2:11.05.02-1.1build1, installed as /usr/bin/pnmtosgi. Allow about ten minutes if your input is ready.

This command writes the SGI data to standard output. That detail controls the whole workflow: give the destination to the shell with >, rather than passing an output filename as an argument. The input file is read, not changed.

1. Check the installed command

Confirm which executable will run and record its Netpbm version:

$ command -v pnmtosgi
/usr/bin/pnmtosgi
$ pnmtosgi --version
pnmtosgi: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pnmtosgi: Built from source dated 2024-03-31 09:09:47

The version information is diagnostic output from this build. Exact build details can differ on another distribution, so keep the local manual page alongside any script that depends on this converter.

Checkpoint

The command exists and reports a Netpbm version. No elevated privileges are needed for this check or for an ordinary conversion.

2. Convert an image without overwriting the destination

Use a new temporary output in the same directory, then rename it after the command succeeds. Replace the two obvious placeholders with paths you control:

input='/path/to/input.ppm'
output='/path/to/output.sgi'
temporary="${output}.new"
pnmtosgi "$input" > "$temporary" && mv -- "$temporary" "$output"
status=$?
if [ "$status" -ne 0 ]; then
    rm -f -- "$temporary"
    exit "$status"
fi

A successful conversion ends with output.sgi in place. The mv only runs when pnmtosgi returns success, so a bad input does not replace a useful existing SGI file.

Safety warning

Plain redirection such as pnmtosgi input.ppm > output.sgi truncates an existing output.sgi before the converter has validated the input. Use the temporary-file pattern when the destination matters. If a failed run leaves output.sgi.new, remove that incomplete file after checking the error; the original output remains untouched.

3. Select the compression mode

-rle is the default. It writes a run length encoded SGI file, which is normally the practical choice for storage. Use -verbatim when you specifically need an uncompressed file, for example when comparing file sizes or testing a consumer that requires that form:

$ pnmtosgi -rle /path/to/input.ppm > /path/to/compressed.sgi
$ pnmtosgi -verbatim /path/to/input.ppm > /path/to/uncompressed.sgi
$ file /path/to/compressed.sgi /path/to/uncompressed.sgi
/path/to/compressed.sgi:   SGI image data, RLE, 3-D, 2 x 1, 3 channels
/path/to/uncompressed.sgi: SGI image data, 3-D, 2 x 1, 3 channels

The two options are alternatives. Do not specify both. If neither is present, expect the RLE form.

4. Set the SGI header name when it helps

Use -imagename to put a short label in the SGI header:

$ pnmtosgi -imagename 'archive preview' /path/to/input.ppm > /path/to/archive-preview.sgi

The name is limited to 79 characters. Without this option, the program writes no name. This field is metadata, not the output filename and not a comment that changes the pixels. Keep shell quoting around names containing spaces.

5. Understand the channel layout

The input PNM family is an abstraction over PBM, PGM and PPM. pnmtosgi preserves the broad image type in the SGI structure: PBM and PGM input produces a two-dimensional, one-channel SGI image, while PPM input produces a three-dimensional, three-channel image.

This distinction is a common source of confusion. A successful command does not turn greyscale input into RGB, and changing -rle to -verbatim does not change the channel count. If a later tool expects RGB, provide it with a PPM input or use a separate, deliberate conversion step before running pnmtosgi.

For a minimal reproducible test, create a tiny PPM in a temporary file and convert it:

printf 'P3\n2 1\n255\n255 0 0 0 255 0\n' > /tmp/pnmtosgi-example.ppm
pnmtosgi -verbatim -imagename 'rgb test' /tmp/pnmtosgi-example.ppm > /tmp/pnmtosgi-example.sgi
file /tmp/pnmtosgi-example.sgi

Expected output identifies an SGI image with dimensions 2 by 1 and three channels. The files under /tmp are disposable test data, not a replacement for a deliberate destination.

6. Verify and recover from failures

Use file for a quick structural check, then use the installed reverse converter if you need to confirm that the pixels can be read back:

$ file /path/to/output.sgi
/path/to/output.sgi: SGI image data, RLE, 3-D, 2 x 1, 3 channels
$ sgitopnm /path/to/output.sgi | head -n 3
sgitopnm: writing PPM image
P6
2 1

The wording from file varies, but the useful evidence is that the file is recognised as SGI and has the expected dimensions and channel count. sgitopnm writes PNM to standard output, so do not send its binary output through a text-only workflow. The head example only displays the PNM header.

If pnmtosgi reports that it cannot open the input, check the path and read permission without changing the image:

ls -l -- /path/to/input.ppm
test -r /path/to/input.ppm && printf '%s\n' 'input is readable'

If the result has the wrong dimensions or channels, check the PNM header and confirm that you supplied the intended source. Do not delete the source while diagnosing. If the command fails after creating a temporary output, remove only that temporary file and rerun after correcting the input. There is no reason to use sudo unless filesystem permissions independently require it; running the converter as root will not repair malformed PNM data.

Done means

  • The installed Netpbm version and input path are known.
  • The SGI output was written through a temporary name before replacement.
  • RLE or verbatim storage was chosen deliberately, with RLE understood as the default.
  • The optional image name is at most 79 characters and is not confused with the filename.
  • file, and where useful sgitopnm, confirm the expected SGI structure.
  • The original PNM and any previous destination remain available for recovery.