Home / Alt manpages / pngtopam(1)

  • pngtopam(1)
  • User command
  • linux

Convert PNG to Netpbm with pngtopam

A downstream tool wants PBM, PGM, PPM or PAM, not PNG, and pngtopam is the Netpbm converter built for exactly that hand-off. You will convert a file, choose what happens to transparency, and check the result without touching 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.

1. Check the command and input

Confirm the converter is installed and the input is readable. Ordinary users can do all of 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: you can feed a PNG on standard input instead, but naming the file makes a repeatable command easier to review later. 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 paper over a path mistake.

2. Convert an opaque PNG and identify the output

With no transparency option, pngtopam discards transparency entirely and picks the Netpbm type from the image: black and white becomes PBM, greyscale becomes PGM, 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. What actually matters is a non-empty destination, the expected dimensions, and a magic number matching the format you picked: P1 or P4 for PBM, P2 or P5 for PGM, P3 or P6 for PPM. Binary output can contain unreadable bytes after the header, so never open it in a text editor.

Redirection is a state-changing shell operation: the shell truncates an existing converted.pnm before pngtopam even starts. If the old file matters, write to a fresh 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 has to stay 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. Depth 4 here means red, green, blue and alpha; a greyscale image normally shows depth 2 instead. Check the header with sed or file, but keep the binary body out of anything terminal-oriented once you are past that header.

Use -alpha instead when you want only the transparency mask. It produces PBM when transparency is strictly on or off, or PGM when the image has graded 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 to each other. Pick one.

4. Flatten transparency against a background

Use -mix when a downstream tool needs an ordinary PBM, PGM or PPM with no transparency channel at all. The default background comes from the PNG's own bKGD chunk; if that chunk is missing, the background is white. Supply -background to pick 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 only works alongside -mix. That colour syntax is Netpbm's own, not a CSS value. To eyeball the result, open flattened.ppm in a PPM-capable viewer or pass it to another Netpbm converter.

5. Inspect unusual PNGs before troubleshooting

Add -verbose to print image properties and conversion details. That text goes to standard error, so the converted image still lands cleanly in 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 the Netpbm PNG writer documents. These options inspect or extract metadata; none of them 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 more capable, but it needs enough memory to hold 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 you use 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 already 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 with a specific display or colour-management reason, and record the value in your processing notes.

If conversion fails, keep the original PNG and read 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 confirming the original is still there:

$ test -s converted.pnm && file converted.pnm
$ rm converted.pnm.new

Warning

That final rm is irreversible and is shown only for a disposable temporary destination. Never substitute the name of your only source image.

Done means

  • Confirmed the setup. pngtopam is installed and the input PNG is readable.
  • Matched format to job. PNM for ordinary pixels, PAM for attached alpha, a mask for -alpha.
  • Decided on transparency. It was either preserved deliberately or flattened against a known background.
  • Verified the output. It is non-empty, with dimensions and header checked.
  • Protected the original. The source PNG stayed untouched, and any replacement was staged under a new name before an explicit move.