Convert PNM Images to PNG with pnmtopng
pnmtopng converts a PNM image to PNG, and a PNM maxval that is not 255 or 65535 can round your colours on the way. You will finish with a verified PNG, plus safe patterns for alpha masks, transparent colours and metadata. The examples use Netpbm 11.5.2 from the installed netpbm package. Allow about ten minutes if the input is ready. The conversion itself normally needs no elevated privileges.
The route
Jump straight to the step you need, or tick off Done means at the end.
pnmtopng reads PNM, PGM or PPM data and writes PNG to standard output. That design is useful in pipelines, but it also means shell redirection can truncate an existing output before the converter has succeeded. Keep the original input and choose a new destination while checking the result.
1. Check the installed command
Confirm which executable will run and record its version:
$ command -v pnmtopng
/usr/bin/pnmtopng
$ pnmtopng -libversion
Pnmtopng version Netpbm 11.5.2.
Pnmtopng Compiled with libpng 1.6.43.
The full version output also includes the zlib version and build details. The manual page installed here is dated 13 March 2019, while the executable is Netpbm 11.5.2. This guide describes the behaviour reproduced by that executable. If another machine has an older Netpbm release, check its local manual before copying a command into a script.
Checkpoint
If command -v prints nothing, install the distribution's Netpbm package through its normal package-management process. Do not use sudo merely to convert an image in a directory you can already read and write.
2. Convert a PNM file without replacing anything
Use a new output name and redirect the PNG there:
$ pnmtopng /path/to/input.ppm > /path/to/output.png
$ file /path/to/output.png
/path/to/output.png: PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced
Your file description may say that the image is grayscale, indexed or 16-bit. The useful checks are that the file is non-empty, it is identified as PNG, and its dimensions match the input. pnmtopng chooses a suitable PNG bit depth: for grayscale input, it can use 1, 2, 4, 8 or 16 bits, so small PNM maxvals can produce a small PNG bit depth.
Do not infer that a successful command has made a visually correct image. Open the PNG or inspect it with another trusted image utility after checking the file type. A PNM image with a low maxval can be mapped to a PNG value with a slightly different numeric sample, even though converting it back at the original precision can recover the source value.
3. Preserve colour precision when it matters
PNG colour components are 8 or 16 bits wide. The converter scales the PNM values to 255 or 65535. If the PNM maxval is not one less than a power of two, low-precision mapping can introduce avoidable rounding. This is most noticeable when an image has a small maxval such as 5.
If exact colour values matter, convert the input to a larger, suitable maxval before creating the PNG. The manual recommends 255 or 65535:
$ pamdepth 255 /path/to/input.pnm > /tmp/input-255.pnm
$ pnmtopng /tmp/input-255.pnm > /path/to/output.png
$ file /path/to/output.png
This creates a temporary intermediate file and leaves the original untouched. Use a temporary directory with suitable permissions for sensitive images. If the image is ordinary 8-bit PPM or PGM already, this extra step is usually unnecessary.
4. Add an alpha mask or transparent colour
For per-pixel transparency, supply a PGM transparency mask with the same image dimensions:
$ pnmtopng -alpha=/path/to/mask.pgm \
/path/to/input.ppm > /path/to/with-alpha.png
$ file /path/to/with-alpha.png
/path/to/with-alpha.png: PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced
Keep the mask as a separate input until the result has been checked. The installed program may represent the transparency as a palette feature when that is smaller, rather than always writing a full PNG alpha channel.
For a single transparent colour, give a colour name or RGB specification:
$ pnmtopng -transparent=white /path/to/input.ppm > /path/to/white-transparent.png
$ pnmtopng -transparent='=rgb:ff/00/0d' /path/to/input.ppm > /path/to/exact-transparent.png
Without the leading equals sign inside the colour value, the command can choose the nearest colour in the image when the requested colour is absent. Prefix the value with = when only an exact match should become transparent. If an exact colour is absent, no colour is made transparent and the command reports that fact.
Checkpoint
Treat these as new files and inspect them before replacing any source or existing export. Transparency is easy to miss in a viewer that displays a checkerboard or applies a background.
5. Choose output format details deliberately
By default, the command selects PNG details that are useful for a compact result. Use -force when the output should stay as close as possible to the Netpbm input: it limits optimisations such as palette conversion and represents a transparency channel as a full channel when possible.
Use -interlace for Adam7 interlacing when a receiver should see a rough, low-resolution representation before the whole image arrives:
$ pnmtopng -interlace /path/to/input.ppm > /path/to/interlaced.png
$ file /path/to/interlaced.png
/path/to/interlaced.png: PNG image data, 1920 x 1080, 8-bit/color RGB, interlaced
Interlacing is not automatically better. It adds passes and is mainly useful for progressive display over slow connections. For ordinary local files, leave it off unless a consuming system asks for it.
For a true PPM or PGM source, -gamma=.52 adds the gamma information associated with PPM. Use it only when the source really has that transfer characteristic. The -size="x y unit" option adds pixel aspect and resolution information, while -modtime="2026-09-26 12:00:00" sets the PNG modification time. Quote values containing spaces.
6. Avoid unsafe replacement and diagnose failures
Shell redirection with > opens the destination before pnmtopng runs. If the destination already exists, it is truncated immediately. Use a temporary output and move it into place only after validation:
$ pnmtopng /path/to/input.ppm > /path/to/output.png.new
$ file /path/to/output.png.new
/path/to/output.png.new: PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced
$ mv /path/to/output.png.new /path/to/output.png
Warning
The final mv replaces the old destination, so run it only after checking the new file. If conversion fails, leave the old output alone and remove the incomplete .new file after inspecting the error. Removing a useful file is irreversible; do not add a blind cleanup command to a batch script.
If the input cannot be opened, check its path and permissions:
$ ls -l /path/to/input.ppm
$ test -r /path/to/input.ppm && echo readable
If the command rejects an option, check man pnmtopng on that machine. Current syntax accepts an equals sign or whitespace between an option and its value, and accepts double hyphens. The older names -chroma, -phys and -time belong to pre-10.30 syntax; use -rgb, -size and -modtime with current Netpbm.
Done means
- Version identified:
pnmtopng -libversionidentified the executable used. - PNG verified: the PNG exists, is non-empty, has the expected dimensions and opens as an image.
- Transparency understood: alpha masks and transparent colours were supplied only when their semantics were understood.
- Options justified:
-force,-interlace, gamma and resolution metadata were selected for a stated reason. - Originals kept: the original PNM and any previous PNG remained available until the new output was verified.