Convert TIFF Files to PNM Safely with tifftopnm
You will convert a TIFF file into a Netpbm image, check the result, and keep a separate copy of the TIFF alpha channel when one exists. The installed command is Netpbm 11.5.2 from package version 2:11.05.02-1.1build1. Allow about fifteen minutes for a normal conversion and a little longer if you need to investigate an unusual TIFF.
The route
Jump straight to the step you need, or tick off Done means at the end.
This guide reads and writes image files only. It does not need sudo. Keep the original TIFF until you have opened or otherwise checked the converted image.
1. Check the installed command
Confirm that the executable comes from the Netpbm installation you expect:
$ command -v tifftopnm
/usr/bin/tifftopnm
$ tifftopnm --version
tifftopnm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
tifftopnm: Built from source dated 2024-03-31 09:09:47
The version text contains build details as well as the library version. The TIFF decoder is provided by the TIFF library, so support depends partly on that library and on the particular TIFF variant. tifftopnm understands common monochrome, greyscale, RGB, RGBA, CMYK and palette TIFFs, but no converter can read every TIFF variation.
Checkpoint: make sure the input path is readable before starting. This check changes nothing:
$ test -r /path/to/input.tif && echo readable
readable
2. Convert a TIFF to the matching PNM type
Pass the TIFF path and redirect standard output to a new destination:
$ tifftopnm /path/to/input.tif > converted.pnm
The output type is selected from the TIFF. A black-and-white image becomes PBM, a greyscale image becomes PGM, and a colour image becomes PPM. The .pnm suffix is deliberately neutral because the actual format is determined by the image. A successful command can still produce an image that is visually wrong, so inspect its header and dimensions:
$ pamfile converted.pnm
converted.pnm: PGM raw, 4 by 2 maxval 255
$ file converted.pnm
converted.pnm: Netpbm image data, size = 4 x 2, rawbits, greymap
Your dimensions and file wording will differ. Look for the correct image type, expected width and height, and a non-empty output. Do not assume that a TIFF filename ending in .tif tells the program which PNM format to write.
3. Preserve an alpha channel explicitly
By default, an input transparency channel is discarded. Add -alphaout when that channel matters. Its value is the name of a separate PGM file:
$ tifftopnm -alphaout converted-alpha.pgm /path/to/input.tif > converted.pnm
$ pamfile converted-alpha.pgm
converted-alpha.pgm: PGM raw, 4 by 2 maxval 255
The alpha output contains greyscale transparency values. If the TIFF has no transparency channel, the manual says the file is still created with all-zero values. Treat that as a useful signal that the input did not provide opacity data, not as proof that the source was RGBA.
There is a special case for -alphaout=-: the alpha PGM is sent to standard output and the ordinary image is discarded. Use it only when a pipeline specifically wants the alpha stream. Do not combine it casually with a redirection intended for the converted colour image.
4. Avoid losing an existing result
Shell redirection with > truncates its destination before tifftopnm starts. Protect a useful existing result by choosing a temporary destination first.
Replace the old name only after checking the conversion status:
$ tifftopnm /path/to/input.tif > converted.pnm.new
$ test "$?" -eq 0 && pamfile converted.pnm.new
converted.pnm.new: PGM raw, 4 by 2 maxval 255
$ mv converted.pnm.new converted.pnm
The mv command changes the destination name, so run it only when the new file is the result you want. If conversion fails, leave the old output alone and inspect the error first.
Remove an incomplete .new file only when you are sure it is disposable. File deletion is irreversible, so do not put cleanup into a blind batch until the workflow is proven.
5. Use standard input for a pipeline
If you omit the TIFF filename, or pass -, tifftopnm reads standard input. The installed version can accept a non-seekable stream, but it may create a temporary regular file containing the complete image first. That needs enough disk space for the input and can remove the performance advantage of a pipe.
$ tifftopnm < /path/to/input.tif > converted.pnm
$ pamfile converted.pnm
converted.pnm: PGM raw, 4 by 2 maxval 255
For a large or untrusted input, account for both the temporary storage and the memory needed by TIFF decoding. A regular file is easier to retry and inspect. Do not feed an unbounded network stream directly into a conversion job without an input-size limit.
6. Choose the conversion method deliberately
Normally, tifftopnm tries to decode the whole image in memory first. This lets the TIFF library handle more formats, but it can use substantial memory and the whole-image path reduces input samples above 8 bits to 8-bit values before scaling them back to the PNM range.
Use -byrow to skip whole-image decoding and process one row at a time:
$ tifftopnm -byrow /path/to/large-or-high-bit-depth.tif > converted.pnm
Row-by-row conversion uses much less memory and can preserve more useful information for some high-bit-depth inputs. It also understands fewer TIFF arrangements, and some compressed formats cannot be read this way. If it fails, retry without -byrow and compare the output rather than assuming that the faster or smaller-memory path is more accurate.
The -orientraw option is specialised. It emits the raw raster order instead of the image orientation described by the TIFF, always using row-by-row conversion. That output is not normally the right picture; use it only when you are deliberately examining raster layout and understand the later pamflip step.
7. Investigate a suspicious TIFF
Ask for diagnostics without changing the input:
$ tifftopnm -verbose -headerdump /path/to/input.tif > converted.pnm
tifftopnm: Converting Image 0
tifftopnm: writing PGM file
tifftopnm: Generating 4w x 2h PNM image
Verbose messages go to standard error, while the PNM remains on standard output. Header dumping adds TIFF directory details such as dimensions, bits per sample, compression, fill order and orientation. The exact lines vary with the file.
By default, the program ignores the TIFF fillorder tag because some real-world files contain an incorrect tag. If you know the tag correctly describes an unusual least-significant-bit-first raster, try -respectfillorder. Compare the resulting image with a trusted reference. Do not add the option merely because a warning appeared; first establish whether the source file's tag is accurate.
Done means
- The installed command reports the expected Netpbm version and the TIFF path is readable.
- The output is a non-empty PBM, PGM or PPM with the expected dimensions.
- An important alpha channel was written separately with
-alphaout. - Existing output was protected from shell redirection until the replacement was checked.
- Memory, bit depth, orientation and fill-order choices were made for the actual TIFF rather than copied blindly.
- The original TIFF remains available for recovery or comparison.