Home / Alt manpages / bmptopnm(1)

  • bmptopnm(1)
  • User command
  • linux

Convert BMP Images to PNM with bmptopnm

Turn a Windows or OS/2 BMP into a Netpbm image that other command-line tools can read, then check which format bmptopnm actually produced. It takes about 10 minutes if the input file already exists, and it needs no elevated privileges.

The examples use Netpbm 11.5.2, from the Debian package netpbm 2:11.05.02-1.1build1.

What bmptopnm produces

bmptopnm reads a BMP from a named file, or from standard input when you give no filename. It writes a PBM, PGM or PPM image to standard output. The output is binary by default, so redirect it to a file instead of reading it in your terminal.

The output type follows the colours in a colour-mapped input:

  • Black and white: becomes PBM.
  • Black, white and grey: becomes PGM.
  • Anything else: becomes PPM.

The image data decides, not the filename extension. A file named picture.bmp can produce output whose header begins with P6, the binary PPM marker.

Checkpoint

The command changes no system configuration and does not overwrite the input unless you choose the same path for output. Keep the output in a new file until you have checked it.

1. Check the local program

Confirm which executable your shell will run and record its Netpbm version:

command -v bmptopnm
bmptopnm --version

On the version used for this guide, the second command reports Netpbm Version 11.5.2 and build information. The manpage documents -verbose, not a separate version interface, so treat version output as implementation-provided diagnostics and not as part of the conversion format.

2. Convert a BMP to a new file

Replace the placeholder path with your BMP. Pick an output name that describes the format only after you have checked the result; .pnm is a safe neutral choice.

bmptopnm /path/to/input.bmp > /path/to/output.pnm

For a colour image you may use output.ppm instead. Do not put the output filename straight after the input without a redirection operator. That would be an extra argument, and the program accepts at most one BMP filename.

Inspect the first three lines without dumping binary pixels to the terminal:

head -n 3 /path/to/output.pnm
file /path/to/output.pnm

Typical headers are P4 for binary PBM, P5 for binary PGM and P6 for binary PPM. The dimensions and maximum value that follow should be readable text. The pixel data after them may contain arbitrary bytes.

Checkpoint

You have a separate output file, its header is P4, P5 or P6, and its dimensions match the source image. If file says the result is a Netpbm image, it is ready for another Netpbm tool.

3. Ask for BMP header diagnostics

Add -verbose when you need to understand the BMP before converting it:

bmptopnm -verbose /path/to/input.bmp > /path/to/output.pnm \
  2> /path/to/bmptopnm-header.txt
sed -n '1,25p' /path/to/bmptopnm-header.txt

The diagnostic stream reports:

  • Class: Windows or OS/2.
  • Geometry: width, height, bits per pixel and row order.
  • Colour map and compression: colour-map size and compression type.
  • Output format: whether the program is writing PBM, PGM or PPM.

Keeping standard error in its own text file stops it mixing into the binary output. Without -verbose, a successful conversion normally leaves standard error quiet.

Warning

Never redirect standard error into the output file. A diagnostic line before the binary pixels corrupts the PNM stream.

4. Use standard input in a pipeline

If another command supplies the BMP, omit the filename and pipe it into bmptopnm:

cat /path/to/input.bmp | bmptopnm > /path/to/output.pnm

For a plain file this is less direct than naming it, but it suits a controlled pipeline. Check the producer's exit status if the pipeline is part of a script. In Bash, enable pipeline failure reporting first:

set -o pipefail
producer-command | bmptopnm > /path/to/output.pnm

None of these examples needs privileges. Only add sudo if the input directory genuinely denies your user access.

Security warning

Giving a converter access to a file does not make the file trustworthy. Process BMPs from untrusted sources in an isolated working directory, and do not open the result with a privileged application.

5. Recover from failures

Some BMP variants are outside this converter's support:

  • RLE4 and RLE8: understood, unless the encoding contains a delta.
  • JPEG or PNG compression: not convertible. The program recognises these and reports an error.
  • OS/2 BMP at 16 bits per pixel: reports an error.

When conversion fails, do not use a partial output file. Write to a new temporary name, check the exit status, and move the file into place only after success:

set -o pipefail
tmp_output=$(mktemp /tmp/bmptopnm-output.XXXXXX)
if bmptopnm /path/to/input.bmp > "$tmp_output"; then
    mv "$tmp_output" /path/to/output.pnm
else
    status=$?
    rm -f "$tmp_output"
    printf 'bmptopnm failed with status %s\n' "$status" >&2
    exit "$status"
fi

Recovery

If you already created a bad output, remove only that generated file, never the original BMP. A failed conversion does not change the source.

Warning

The mv replaces an existing destination. Use a fresh destination, or check it first, if preserving the old file matters.

Version history explains differences on older systems. Windows Version 4 and Version 5 BMP support arrived before Netpbm 10.75. RLE8 support arrived before 10.32, while reliable RLE4 support needs Netpbm 10.85 or later. Netpbm 11.5.2 is newer than all of those, but JPEG- or PNG-compressed BMP input remains unsupported.

Done means

  • Version known. bmptopnm --version identifies the local Netpbm installation.
  • Input untouched. The BMP is unchanged and the output is a separate file.
  • Header valid. head -n 3 or file confirms a PBM, PGM or PPM header.
  • Diagnostics kept apart. Verbose output went to standard error, not into the binary image.
  • Failures safe. Unsupported compression or bit depth failed without leaving a file you could mistake for a valid conversion.