Convert Netpbm Images to TIFF with pamtotiff
pamtotiff turns a PNM or PAM image into a TIFF file, but its defaults quietly append to files instead of overwriting them. This guide gets you a checked TIFF with a deliberate output path, compression and colour representation. The examples use the Netpbm 11.5.2 package installed on Ubuntu and take about five minutes if your input is ready.
The route
Jump straight to the step you need, or tick off Done means at the end.
Before you start
- You need: the
pamtotiffcommand and a PNM or PAM input such as a PBM, PGM or PPM file. - No root needed: reading your own input and writing to a directory you control does not need elevated privileges. Do not use
sudojust to convert an image. - Choose a fresh output path. This command creates or changes the named output file, so pick a new path or back up an existing TIFF before you start.
Checkpoint
The normal workflow is input image, explicit output file, then an independent file check.
1. Check the installed command
Confirm which executable will run and record its Netpbm version. This matters when copying a command to an older host: the current output-file interface arrived in Netpbm 10.67, and older releases behaved differently.
$ command -v pamtotiff
/usr/bin/pamtotiff
$ pamtotiff --version
pamtotiff: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
Your build may print extra lines after the version. If the command is missing, install the distribution's netpbm package through its normal package-management process, then repeat this check.
2. Convert one image to a new TIFF
Use -output when you want pamtotiff to write directly to a regular file. The file must be readable and seekable; this avoids the extra temporary-file copy used when the TIFF goes to standard output.
$ pamtotiff -output '/path/to/result.tif' '/path/to/source.pnm'
$ file '/path/to/result.tif'
/path/to/result.tif: TIFF image data, ...
Replace both placeholders with real paths, and keep the quotes: they protect spaces and shell metacharacters. Here is the trap: if result.tif already contains data, the current program appends another TIFF image to it rather than replacing it. That is a significant default to know before you rely on it.
For a one-off pipeline, standard output is also valid:
$ pamtotiff '/path/to/source.pnm' > '/path/to/result.tif'
$ file '/path/to/result.tif'
Warning
Do not redirect into the same file you are reading from. The shell truncates the destination before pamtotiff can read it, destroying the source. If a conversion fails, check both paths first: pamtotiff normally reports the first file or format problem on standard error.
3. Choose compression deliberately
The default is an uncompressed TIFF, predictable and widely readable but sometimes bigger than it needs to be. Pick one compression method when size matters:
$ pamtotiff -flate -output '/path/to/result-flate.tif' '/path/to/source.pnm'
$ pamtotiff -lzw -output '/path/to/result-lzw.tif' '/path/to/source.pnm'
$ file '/path/to/result-flate.tif' '/path/to/result-lzw.tif'
-flateuses the zlib-based compression also used by PNG.-lzwis available on this installation, but a TIFF library on an older system may not provide it.- Other choices:
-none,-packbits,-g3,-g4and-adobeflate. Group 3 and Group 4 are for bilevel or fax-like data, not ordinary colour photographs. -2dand-fillonly apply with Group 3.
Checkpoint
Use file, or your receiving application's import test, to confirm the chosen compression is accepted before you replace an original.
4. Control colour output when it matters
PBM and PGM input produce grayscale TIFF. A PPM image with a few colours may become a paletted TIFF; push past 256 colours and it becomes RGB. Use -color to force colour output for a PPM that happens to contain only grey shades. Add -truecolor when you need 24-bit RGB instead of a palette:
$ pamtotiff -color -truecolor -output '/path/to/rgb.tif' '/path/to/source.ppm'
$ file '/path/to/rgb.tif'
/path/to/rgb.tif: TIFF image data, ... PhotometricInterpretation=RGB, ...
These two options also skip the extra input pass Netpbm uses to analyse PPM colours, which helps on a large file. For an ordinary grayscale scan they add nothing. Do not use -miniswhite or -minisblack on genuinely colour data: those photometric settings are for grayscale images.
5. Add another image only after making a backup
A TIFF can contain multiple images. With the current interface, -append needs the output file to already exist; it will not create a new one.
$ cp --preserve=all '/path/to/existing.tif' '/path/to/existing.tif.bak'
$ pamtotiff -append -output '/path/to/existing.tif' '/path/to/second.pnm'
$ file '/path/to/existing.tif'
This changes the TIFF in place and can make it unsuitable for an application that expects a single image. The backup is your recovery path: if the result is wrong, restore it with cp --preserve=all '/path/to/existing.tif.bak' '/path/to/existing.tif'. If the target does not exist yet, leave out -append for the first image.
Common traps
- No TIFF identified? Check that the input is a valid PNM or PAM and that the output was not truncated by a failed redirection.
- Extra image appeared in an existing TIFF?
-outputappends to a non-empty file by default. Convert to a new filename instead. - Colour file looks grayscale? Check whether the PPM really contains more than grey values, then retry with
-color, adding-truecolortoo if a palette is not acceptable. - Large PPM converting slowly? pamtotiff may be reading it twice to build a palette. The paired
-color -truecoloroptions skip that analysis. - Do not assume TIFF variants are interchangeable. The TIFF library controls details of the generated file, so test the actual consumer that will open it.
Done means
- Version checked:
pamtotiff --versionidentified the expected Netpbm installation. - Output confirmed: the TIFF sits at the intended path, checked with
fileor the receiving application. - Compression and colour mode chosen to match the consumer's requirements.
- No accidental overwrite: nothing was clobbered, and any deliberate append has a recoverable backup.