Home / Alt manpages / pnmtorle(1)

  • pnmtorle(1)
  • User command
  • linux

Convert Netpbm Images to RLE Safely with pnmtorle

By the end of this guide, you will have converted a Netpbm image such as PPM or PGM into a Utah RLE file, checked its header without writing output, and verified that the result can be read back. Allow about 10 minutes if Netpbm is already installed. The examples use ordinary user permissions and write only to a file you name.

Before you start

Install the Netpbm tools supplied by your distribution, and have a PPM, PGM or PBM input file ready. This guide was checked with Netpbm 11.5.2 from Debian's netpbm package. Check your own executable before relying on version-specific behaviour:

command -v pnmtorle
pnmtorle --version

The command accepts one input filename. If you omit it, it reads Netpbm data from standard input. Its output is binary RLE, so do not print it in a terminal or redirect it into a text file by accident.

Checkpoint

You should know the input path and the exact output path before converting. No command below needs sudo. If the input is shared or valuable, work from a copy; conversion itself does not alter the input.

1. Inspect the input without converting it

Use -header when you need to confirm what Netpbm sees. It writes a short description to standard error and does not produce an RLE file:

pnmtorle -header /path/to/input.ppm

For a plain PPM test image, the installed program reports details such as the image type, dimensions and maximum value. The command exits with status zero when the header was read successfully. Capture both output streams if you are scripting around it:

pnmtorle -header /path/to/input.pgm >/dev/null
printf 'exit status: %s\n' "$?"

Do not confuse -header with -verbose. Both print header information, but -verbose continues with conversion. Since Netpbm 10.98, the long option names are -header and -verbose; older releases used -h and -v.

2. Convert to an explicitly named RLE file

Give -outfile a destination and keep diagnostics separate from the binary data:

pnmtorle -verbose /path/to/input.ppm -outfile=/path/to/output.rle

The equals sign is optional, so -outfile /path/to/output.rle is equivalent on current Netpbm. The input can appear before or after the option, as shown by the documented form. With a colour input, the RLE result has three colour channels and 24 bits. Grayscale or black-and-white input produces one grayscale channel and 8 bits.

Verify that a file was created and that it is recognised as RLE:

test -s /path/to/output.rle && file /path/to/output.rle

On the checked installation, a successful conversion returned status zero, wrote no binary data to standard output, and file identified the result as RLE image data. If the destination already exists, treat replacement as destructive: choose a new name or make a backup first. pnmtorle has no undo command. Removing a newly created output is safe only after you have confirmed it is not needed.

3. Use standard input or standard output deliberately

With no input filename, the program reads standard input. With no -outfile, it writes RLE to standard output. This makes pipelines useful, but it also makes a misplaced command easy to miss:

pnmtoppm /path/to/source.png | pnmtorle -outfile=/path/to/output.rle

The example assumes that pnmtoppm can read the source format on your system. If you want to preserve the conversion's diagnostics, leave standard error alone. Never pipe the RLE stream through a text filter. To test the standard-input path with an existing Netpbm file:

pnmtorle </path/to/input.pgm >/path/to/output.rle
file /path/to/output.rle

If you set -outfile=-, standard output is also selected explicitly. That is useful when the next program in a pipeline expects binary RLE. It is not a request to print a readable report.

4. Verify the RLE by converting it back

Netpbm's companion rletopnm can read the result and write a Netpbm image. This checks that the file is structurally readable, although it is not a pixel-for-pixel comparison of every metadata detail:

rletopnm /path/to/output.rle >/path/to/roundtrip.ppm
file /path/to/roundtrip.ppm

A successful round trip should return status zero and identify the new file as Netpbm image data. If the command fails, keep the original input and inspect the first conversion's exit status and diagnostics. Do not replace the source with an unverified result.

5. Treat -alpha as a compatibility test

-alpha asks for an additional transparency channel. Its rule is unusual: black pixels become transparent and every other pixel becomes fully opaque. This is not a general-purpose alpha channel copied from an input image.

There is also a version boundary worth checking. On the installed Netpbm 11.5.2 executable used for this guide, -alpha aborted on small PPM and PGM test files, with exit status 139 or 134 and, in one case, an invalid-pointer diagnostic. That is a program failure, not evidence that the input was converted successfully.

Do not use -alpha in an unattended conversion until it passes a test with your exact Netpbm build and representative input:

set +e
pnmtorle -alpha /path/to/input.ppm -outfile=/tmp/test-output.rle
status=$?
set -e
printf 'pnmtorle status: %s\n' "$status"
test "$status" -eq 0 && test -s /tmp/test-output.rle

This test writes only under /tmp. If it fails, remove the test file after collecting the diagnostic, update Netpbm through your normal package process, or avoid the option and report the reproducible failure to the package maintainer. Do not grant extra privileges to work around a crash.

Common traps

  • RLE in the terminal: binary output may corrupt the display. Always name an output file or pipe it to a program that expects RLE.
  • Header output mistaken for conversion: -header deliberately skips conversion. Use -verbose when you need both diagnostics and an output file.
  • Wrong channel expectations: colour becomes three-channel RLE; grayscale and black-and-white become single-channel RLE. The tool does not preserve an arbitrary source alpha channel.
  • Old tutorials: -o, -h and -v describe pre-10.98 spellings. Prefer the current long names, especially when copying commands between machines.
  • Multiple-image input: the output can contain several concatenated RLE images. A downstream tool must support that form; a successful exit does not mean there is only one image.

Done means

  • pnmtorle --version identified the Netpbm build you tested.
  • -header read the intended input without creating output.
  • The conversion returned status zero and file recognised the destination as RLE.
  • rletopnm read the RLE back successfully, if that tool is available.
  • You tested -alpha separately, or left it disabled because your build fails with it.