Convert PNG Files to Netpbm with pngtopam
You will convert a PNG into a PBM, PGM, PPM or PAM file, choose what happens to transparency, and check the result without changing the original image. Allow about ten minutes for one file. You need the netpbm package and a readable PNG. The examples use Netpbm 11.5.2, installed here as package version 2:11.05.02-1.1build1; other releases can differ in small details.
The route
Jump straight to the step you need, or tick off Done means at the end.
1. Check the command and input
Start by confirming that the converter is installed and that your input is readable. Ordinary users can do this without elevated privileges:
$ command -v pngtopam
/usr/bin/pngtopam
$ file /path/to/input.png
/path/to/input.png: PNG image data, 248 x 87, 8-bit/color RGBA, non-interlaced
Replace /path/to/input.png with an actual file. The input argument is optional, so you can also feed a PNG on standard input, but naming the file makes a repeatable command easier to review. The program writes the converted image to standard output.
Checkpoint: if file reports that the path does not exist or is not readable, fix the path or its ordinary read permission first. Do not run the converter as root just to hide a path mistake.
2. Convert an opaque PNG and identify the output
With no transparency option, pngtopam discards transparency information. It selects the Netpbm type from the image: black and white becomes PBM, greyscale becomes PGM, and colour becomes PPM. Redirect standard output to a new destination:
$ pngtopam /path/to/input.png > converted.pnm
$ file converted.pnm
converted.pnm: Netpbm image data, size = 248 x 87, rawbits, pixmap
$ head -c 2 converted.pnm
P6
The exact file wording varies. The useful checks are a non-empty destination, expected dimensions, and a magic number matching the selected format: P1 or P4 for PBM, P2 or P5 for PGM, and P3 or P6 for PPM. Binary output can contain unreadable bytes after its header, so do not open it in a text editor.
Redirection is a state-changing shell operation. The shell truncates an existing converted.pnm before pngtopam starts. If the old file matters, use a new name such as converted.pnm.new, verify it, then replace the old file deliberately.
3. Keep transparency in a PAM file
Use -alphapam when the alpha channel must remain attached to the image. The output is always PAM, with tuple type GRAYSCALE_ALPHA for greyscale input or RGB_ALPHA for colour input:
$ pngtopam -alphapam /path/to/input.png > converted.pam
$ sed -n '1,7p' converted.pam
P7
WIDTH 248
HEIGHT 87
DEPTH 4
MAXVAL 255
TUPLTYPE RGB_ALPHA
ENDHDR
The header is text, followed by binary samples. The depth of 4 in this example means red, green, blue and alpha. A greyscale image will normally show depth 2 instead. Check the header with sed or file, but keep the binary file out of terminal-oriented tools after the header.
Use -alpha instead when you want only the transparency mask. It produces PBM when transparency is effectively on or off, or PGM when the image has different transparency levels:
$ pngtopam -alpha /path/to/input.png > transparency.pgm
$ file transparency.pgm
transparency.pgm: Netpbm image data, size = 248 x 87, rawbits, greymap
-alphapam, -alpha and -mix are alternatives. Choose only one.
4. Flatten transparency against a background
Use -mix when a downstream tool needs a normal PBM, PGM or PPM without a transparency channel. The default background comes from the PNG's bKGD chunk. If that chunk is absent, the background is white. Supply -background to choose a colour explicitly:
$ pngtopam -mix -background=rgb:01/ff/80 /path/to/input.png > flattened.ppm
$ file flattened.ppm
flattened.ppm: Netpbm image data, size = 248 x 87, rawbits, pixmap
-background is valid only with -mix. The colour syntax above is a Netpbm colour name, not a CSS value. If you want to test the result visually, open flattened.ppm in an image viewer that supports PPM or pass it to a separate Netpbm converter.
5. Inspect unusual PNGs before troubleshooting
Add -verbose to print image properties and conversion details. Diagnostic text goes to standard error, so the converted image can still go to a file:
$ pngtopam -verbose /path/to/input.png > converted.pnm 2> pngtopam.log
$ sed -n '1,4p' pngtopam.log
pngtopam: reading a 248 x 87 image, 8 bits
pngtopam: truecolor+alpha, not interlaced, base filter
pngtopam: tRNS chunk (transparency): not present
pngtopam: gAMA chunk (image gamma): not present
Use -time if you specifically need the PNG's tIME chunk printed to standard error. Use -text=/path/to/metadata.txt to save tEXt and zTXt chunks in the format described by the Netpbm PNG writer documentation. These options inspect or extract metadata; they do not make a lossy PNG conversion reversible.
6. Handle memory, gamma and conversion failures
By default, the installed program uses the whole-image method. It is generally the more capable path, but it needs enough memory for the decoded image. Add -byrow for a lower-memory row-by-row conversion:
$ pngtopam -byrow /path/to/large.png > large.pnm
$ file large.pnm
The manual warns that row-by-row conversion can behave differently for some PNG features, especially interlaced images, so verify dimensions and inspect the result when using it as a recovery path. It is not a resize operation.
Gamma handling is a common source of surprising colours. Without -gamma, pngtopam ignores PNG gAMA information by default and assumes the PNG uses the same gamma transformation as a Netpbm image. Supplying -gamma=value asks the PNG library to apply a screen-gamma correction, which changes the output samples. Use it only when you have a specific display or colour-management reason, and record the value in your processing notes.
If conversion fails, keep the original PNG and inspect the diagnostic text. Check the input with file, rerun with -verbose, and try -byrow only when memory or decoder behaviour is the suspected cause. Remove an incomplete new output after checking that the original is still present:
$ test -s converted.pnm && file converted.pnm
$ rm converted.pnm.new
The final rm is irreversible and is shown only for a disposable temporary destination. Never substitute the name of your only source image.
Done means
pngtopamis installed and the input PNG is readable.- The output format matches the job: PNM for ordinary pixels, PAM for attached alpha, or a mask for
-alpha. - Transparency was either preserved deliberately or flattened with a known background.
- The output is non-empty and its dimensions and header have been checked.
- The original PNG remains untouched, and any replacement was staged under a new name before an explicit move.