Convert a Netpbm Image to TIFF with pnmtotiff
You will convert a PNM or PAM image into a TIFF file, check that the output is really TIFF, and understand when to replace the old command with pamtotiff. Allow about ten minutes for a single conversion. The examples use ordinary user privileges and leave the source image untouched.
The route
Jump straight to the step you need, or tick off Done means at the end.
1. Check the installed Netpbm version
The local pnmtotiff manual is unusually short: it identifies the program as replaced by pamtotiff and says that pamtotiff is backward compatible with it. That means you should treat pnmtotiff as a compatibility command, not as the place to look for new TIFF features.
Check which executable your shell will run and record the package version:
$ command -v pnmtotiff
/usr/bin/pnmtotiff
$ pnmtotiff --version
pnmtotiff: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
...
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
Your package version and build details can differ. The commands below describe the Netpbm 11.5.2 installation used for this guide. If command -v prints nothing, install Netpbm through your distribution's normal package manager before continuing. Do not use sudo merely to convert an image.
2. Convert an image through standard input and output
Use shell redirection to make the data flow explicit. The input below is a placeholder path: replace it with a readable PNM or PAM file. The output path should be new or deliberately disposable.
$ pnmtotiff < /path/to/input.pnm > output.tif
This does not alter input.pnm. The TIFF bytes go to standard output, which the shell writes to output.tif. A small RGB PPM can produce diagnostic messages such as these on standard error while still completing successfully:
pnmtotiff: computing colormap...
pnmtotiff: 2 colors found
Those messages are not the TIFF image. They remain separate from the redirected output, so do not judge success by whether the terminal stayed silent.
3. Verify the result before using it
Check the exit status immediately, then inspect the file type and size:
$ test -s output.tif && echo "non-empty output"
non-empty output
$ file output.tif
output.tif: TIFF image data, little-endian, ...
The exact file wording depends on the file utility and the input. The useful result is that it identifies TIFF and reports the expected dimensions or colour information. A zero-byte file, a non-zero exit status, or a result identified as PNM means the conversion did not finish as intended. Keep the source until you have opened or further processed the TIFF.
For a reproducible test without an existing image, feed a tiny PPM directly to the command. This is a test of the installed converter, not a replacement for checking your real input:
$ pnmtotiff < <(printf 'P3\n2 1\n255\n255 0 0 0 255 0\n') > /tmp/pnmtotiff-test.tif
$ file /tmp/pnmtotiff-test.tif
/tmp/pnmtotiff-test.tif: TIFF image data, little-endian, ... width=2
The process substitution syntax requires Bash or a compatible shell. The temporary TIFF is safe to remove after checking it:
$ rm /tmp/pnmtotiff-test.tif
4. Prefer pamtotiff for new scripts
Netpbm introduced the replacement in version 10.30, released in October 2005. The current command is pamtotiff, which reads PNM or PAM input and can also handle a stream containing multiple Netpbm images. Use it in new scripts so the command name matches current documentation:
$ pamtotiff /path/to/input.pnm > output.tif
$ file output.tif
output.tif: TIFF image data, ...
With current Netpbm, pamtotiff also accepts -output when you need the program to write a seekable TIFF file directly:
$ pamtotiff -output output.tif /path/to/input.pnm
The installed pamtotiff manual says standard output is the default. It also says that an existing non-empty file named by -output receives another image, because TIFF can contain multiple images. That is an append-like state change, so do not point -output at a valuable TIFF until you have confirmed the destination and intended behaviour. Use a new filename when you want one image only.
5. Choose compression deliberately
The replacement command creates uncompressed TIFF by default. That is predictable and is the safest first conversion, but it can produce a large file. If your reader supports it, choose a documented compression option explicitly:
$ pamtotiff -flate /path/to/input.pnm > output-flate.tif
$ file output-flate.tif
output-flate.tif: TIFF image data, ...
The -flate option uses the compression implemented by the zlib library. Other documented choices include -packbits, -lzw, -g3, -g4 and -adobeflate, but TIFF readers do not all support every choice equally. Start with the default or -flate, then verify the output in the application that will consume it. Do not add old options from an unrelated TIFF converter: pnmtotiff and pamtotiff have their own option set.
6. Avoid the redirection traps
Shell redirection with > truncates an existing destination before pnmtotiff starts. If conversion fails, that can destroy a previous TIFF. Use a temporary destination, verify it, then replace the old file only when you are ready:
$ pamtotiff /path/to/input.pnm > output.tif.new
$ test -s output.tif.new && file output.tif.new
$ mv output.tif.new output.tif
The final mv changes the name in the same directory and is normally an atomic replacement. If the check fails, leave the old file alone and investigate the input or error message. To recover from a failed conversion before the final move, remove only output.tif.new. If you accidentally replaced a file, recover it from your normal backup rather than rerunning a command against the same destination.
Done means
- The installed Netpbm version and executable path are known.
- The source PNM or PAM file remains unchanged and readable.
- The converter exits successfully and produces a non-empty file identified as TIFF.
- New scripts use
pamtotiff;pnmtotiffis retained only where compatibility makes that useful. - Any compression or direct output-file behaviour was chosen deliberately and checked with the consuming application.