Convert PNG to PNM the Old Way with pngtopnm
An old script calls pngtopnm and still works, but you are wondering whether you should be using something newer. Short answer: pngtopnm is a compatibility name, kept working but not the tool to reach for on purpose any more. This guide gets you a reproducible conversion, a way to check the result, and a clear view of what happens to transparency. The command ships as part of Netpbm 11.5.2 on the system used here. Allow about ten minutes. You need a shell, a readable PNG file, and enough disk space for the uncompressed output.
The route
Jump straight to the step you need, or tick off Done means at the end.
Netpbm introduced pngtopam in version 10.44, and from version 10.48 pngtopnm became an alias for it. The older name still turns up in scripts and old instructions, but new work should normally reach for pngtopam instead, especially when you need a PAM file with an alpha channel attached.
1. Check the installed command
Confirm which executable you are actually running, and which Netpbm release it reports:
$ command -v pngtopnm
/usr/bin/pngtopnm
$ pngtopnm --version
pngtopnm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
That version output carries build details alongside the release number, and your path or release may differ. If the command is missing, install the Netpbm package through your distribution's own package manager; that is an administrative action, so it is deliberately not offered here as a copy-and-paste line.
Checkpoint
Only continue once command -v finds the expected program. Do not assume a similarly named tool from another image package accepts the same options.
2. Convert a PNG to PNM
Use an explicit input path and redirect standard output to a new destination, swapping in your own file names:
$ pngtopnm /path/to/input.png > /tmp/output.pnm
$ pnmfile /tmp/output.pnm
/tmp/output.pnm: PPM raw, 16 by 16 maxval 255
Leave out an option and the foreground image gets written with transparency discarded. The output subtype follows the input: black and white becomes PBM, greyscale becomes PGM, colour becomes PPM. A PNM file is uncompressed, so check the available space before converting a large image.
That redirection creates or replaces /tmp/output.pnm, the first destructive step in this workflow. Do not point it at an original image or an output you actually need. To undo the example, remove only the generated file after checking its path:
$ test /tmp/output.pnm && rm -- /tmp/output.pnm
rm is irreversible for that pathname. The safer habit is a fresh output name for every trial run.
3. Verify conversion and failure handling
Check the PNM header without opening it in an image viewer:
$ pnmfile /tmp/output.pnm
/tmp/output.pnm: PPM raw, 16 by 16 maxval 255
Dimensions and subtype will match your own image. A valid PNM header starts with P1 through P6; the common raw colour result is P6. In a script, test the converter before trusting the output:
if pngtopnm -- /path/to/input.png > /tmp/output.pnm; then
pnmfile /tmp/output.pnm
else
status=$?
printf 'pngtopnm failed with status %s\n' "$status" >&2
exit "$status"
fi
The double hyphen separates options from the input name, which matters when a path starts with a hyphen, though an absolute path is usually clearer anyway. If conversion fails, do not pass the partial file to another program. Write to a temporary name in the same directory, validate it, and rename it only if the next stage needs an atomic hand-off.
Checkpoint
The converter has to return status 0, and pnmfile has to report the dimensions and format you expect. A clean exit does not by itself prove colour management or transparency requirements were met.
4. Choose how transparency is handled
The legacy command has three mutually exclusive modes:
- No mode at all. Only the foreground image is written; transparency is lost.
--alpha. Writes the transparency channel or mask as PBM or PGM. The main image is not written.--mix. Composites the foreground against a background and writes the result as PNM.
For a PNG with an alpha channel, pull that channel out separately when another tool needs a mask:
$ pngtopnm --alpha /path/to/input.png > /tmp/mask.pgm
$ pnmfile /tmp/mask.pgm
/tmp/mask.pgm: PGM raw, 16 by 16 maxval 255
The subtype can come out as PBM when transparency is only ever on or off. This command will not give you one file holding both colour and alpha together; that is the job of the newer pngtopam --alphapam option:
$ pngtopam --alphapam /path/to/input.png > /tmp/with-alpha.pam
$ head -n 7 /tmp/with-alpha.pam
P7
WIDTH 16
HEIGHT 16
DEPTH 4
MAXVAL 255
TUPLTYPE RGB_ALPHA
ENDHDR
Only expect that exact header for an RGBA PNG. A greyscale image produces GRAYSCALE_ALPHA and a different depth. Prefer this PAM route on current Netpbm whenever you need to keep transparency for later processing.
5. Composite against a known background
--mix uses the PNG's own bKGD chunk when one is present. With no such chunk, the default background is white. Set it explicitly whenever the colour actually matters:
$ pngtopnm --mix --background=rgb:01/ff/80 /path/to/input.png > /tmp/composited.ppm
$ pnmfile /tmp/composited.ppm
/tmp/composited.ppm: PPM raw, 16 by 16 maxval 255
Netpbm's own colour parser handles that syntax; the three hexadecimal components above are red, green and blue. --background requires --mix on current releases, and supplying it alone is an error, so keep the pair together in scripts.
Warning
Compositing is not a reversible way to preserve an alpha channel. Once the foreground has been mixed with a background, the original transparency cannot be recovered from that PPM alone. Keep the source PNG if you might need a different background later.
6. Handle gamma and metadata deliberately
By default, Netpbm ignores the PNG's image-gamma information and treats the samples as already suitable for Netpbm output. --gamma=value asks the PNG library to apply gamma handling, but it also applies a display-oriented screen-gamma transformation that can change pixel values in surprising ways inside a data pipeline. Use it only with a defined colour requirement, and compare the result against a known reference.
To inspect the input while converting, add --verbose. It reports dimensions, colour type and PNG chunks on standard error while the PNM keeps going to standard output. Use --text=/path/to/metadata.txt to write PNG text chunks to a separate file, and --time to print the PNG time chunk. None of these options put PNG metadata into the PNM pixels themselves.
$ pngtopnm --verbose /path/to/input.png > /tmp/output.pnm 2> /tmp/pngtopnm.log
$ sed -n '1,12p' /tmp/pngtopnm.log
pngtopnm: reading a 16 x 16 image, 8 bits
pngtopnm: truecolor+alpha, not interlaced, base filter
Keep diagnostic output separate from binary image data. Mixing standard error into the redirected PNM with 2>&1 can corrupt the file.
Done means
- Confirmed the version. The installed Netpbm release and executable path are known.
- Converted deliberately. The PNG went to a deliberately named PNM destination.
- Verified the output.
pnmfileconfirmed subtype, dimensions and maxval. - Handled transparency on purpose. It was intentionally discarded, extracted, composited, or retained in a PAM file.
- Treated the tricky bits as decisions. Gamma, metadata, binary redirection and output replacement were each explicit choices, not accidents.