Convert PPM Images to Sun Rasterfiles with pnmtorast
pnmtorast turns a PPM image into a Sun rasterfile, and choosing the right encoding up front saves a second conversion later. Allow about ten minutes. You need a shell, a readable PPM file and the netpbm package.
The route
Jump straight to the step you need, or tick off Done means at the end.
These commands normally run as an unprivileged user; use sudo only if your input or destination directory is deliberately restricted.
1. Check the installed command
This guide follows Netpbm 11.05.02, provided here by package version 2:11.05.02-1.1build1. The installed manual page is old, so the version check matters when you are comparing another host or writing a script.
$ command -v pnmtorast
/usr/bin/pnmtorast
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
On a non-Debian system, use that system's package query command instead. The useful checkpoint is that command -v returns a real executable and the package is installed. The program's help message points back to its manual page rather than listing options, so use man pnmtorast for the local reference.
2. Convert a PPM to the default raster format
Give the input file as the optional positional argument and redirect standard output to a new destination:
$ pnmtorast /path/to/input.ppm > /path/to/output.ras
- Default is compressed.
-rleis on by default and produces the Sun rasterfileRT_BYTE_ENCODEDform. It is smaller than the standard form, but the manual describes it as less standard. - Source is read-only. The source PPM is read, not rewritten. Image data goes to standard output, while this installed build can print a short colour-map diagnostic on standard error.
Check the exit status immediately and inspect the file:
$ printf '%s\n' "$?"
0
$ file /path/to/output.ras
/path/to/output.ras: Sun raster image data, 2 x 1, 8-bit, compressed, RGB colormap
Your file wording will vary with the image and the version of the file utility. Confirm that the command returned zero, that the destination is non-empty, and that its reported dimensions match the input.
3. Request the standard Sun format when compatibility needs it
Use -standard when a consumer expects RT_STANDARD rather than the default byte-encoded form:
$ pnmtorast -standard /path/to/input.ppm > /path/to/output-standard.ras
$ file /path/to/output-standard.ras
/path/to/output-standard.ras: Sun raster image data, 2 x 1, 8-bit, RGB colormap
There is no image resize here. The option changes the rasterfile encoding. You can spell the default explicitly with -rle:
$ pnmtorast -rle /path/to/input.ppm > /path/to/output-compressed.ras
Do not assume that a file extension selects the format. The option and the input command select the behaviour; the extension is only a naming convention.
4. Let a pipeline provide the PPM
The input argument is optional. If it is absent, pnmtorast reads the PPM from standard input, which lets another Netpbm command feed it:
$ pnmscale -width 800 /path/to/input.ppm | pnmtorast > /path/to/scaled.ras
This example requires pnmscale and changes the image before raster conversion. Verify each stage when diagnosing a failure. A pipeline's final status can hide an earlier failure in shells without pipeline failure handling, so run the producer separately or enable your shell's pipeline error option for a script.
For a simple round-trip check, use the companion converter if it is installed:
$ rasttopnm /path/to/output.ras | sed -n '1,3p'
P6
2 1
255
The PPM header should report the expected width and height. Netpbm raster output uses eight-bit colour values, so pnmtorast scales colour values to a maximum of 255 automatically. An extra pamdepth step is not required for that purpose.
5. Avoid overwriting a useful rasterfile
Shell redirection with > truncates an existing destination before pnmtorast starts. Choose a new filename while testing. For a deliberate replacement, write a temporary sibling and move it into place only after conversion succeeds:
$ pnmtorast /path/to/input.ppm > /path/to/output.ras.new && \
mv -- /path/to/output.ras.new /path/to/output.ras
This leaves the old destination in place if conversion fails. The final mv replaces an existing file, so check the two paths before running it. If a failed conversion leaves the temporary file, inspect it if you need the diagnostic, then remove only that temporary pathname. Do not remove the original PPM as part of cleanup.
Common traps
- Input will not open. Check the path and read permission with
ls -l /path/to/input.ppmandtest -r /path/to/input.ppm. Do not addsudountil you know the directory policy. - A reader rejects the result. Try
-standard. The default is compressed and is explicitly less standard. - Unexpected dimensions or colours. Verify the source PPM and inspect the round trip with
rasttopnm. A successful exit status confirms that output was produced; it does not prove that the source image was the one you intended. - Need quiet operation. The common libnetpbm options include
-quiet. Confirm support with the installed manual before depending on it in portable code.
Done means
- Version confirmed:
pnmtorastis installed and its version is known. - Conversion succeeded: it returned status zero and created a non-empty Sun rasterfile.
- Format chosen deliberately: you picked
-standardfor compatibility or kept the smaller default-rleform knowingly. - Output verified: the reported dimensions and, when needed, a
rasttopnmround trip match the intended image. - Source protected: the original PPM remains available, and any replacement was staged through a temporary output.