Home / Alt manpages / bmptopnm(1)

  • bmptopnm(1)
  • User command
  • linux

Convert BMP Images to PNM Safely with bmptopnm

You will turn a Windows or OS/2 BMP file into a Netpbm image that other command-line tools can read, while checking which format was actually produced. The examples use Netpbm 11.5.2, installed from the Debian package netpbm 2:11.05.02-1.1build1. Allow about 10 minutes if the input file already exists. The conversion itself does not need elevated privileges.

What bmptopnm produces

bmptopnm reads a BMP from a named file, or from standard input when no filename is supplied, and writes a PBM, PGM or PPM image to standard output. The output is binary by default, so redirect it to a file rather than trying to read it in your terminal.

The output type follows the colours in a colour-mapped input. A black-and-white image becomes PBM. An image containing black, white and grey becomes PGM. Other colour images become PPM. The decisive detail is the image data, not the filename extension. A file named picture.bmp can therefore produce a file whose header begins with P6, the binary PPM marker.

Checkpoint

The command changes no system configuration and does not overwrite the input unless you explicitly 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 the -verbose option, not a separate version interface, so treat version output as implementation-provided diagnostic information rather than as part of the conversion format.

2. Convert a BMP to a new file

Replace the placeholder path with the BMP you want to convert. Use a filename that describes the output 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 instead use output.ppm. Do not put the output filename immediately after the input without a redirection operator: that would be an extra argument, and this 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 following dimensions and maximum value should be readable text. The pixel data follows them and may contain arbitrary bytes.

Checkpoint

You have a separate output file, its header is one of 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 details such as the Windows or OS/2 class, width, height, bits per pixel, row order, colour-map size and compression. It also says whether the program is writing a PBM, PGM or PPM image. Keeping standard error in its own text file makes it easier to inspect without mixing it into the binary output.

Without -verbose, a successful conversion normally leaves standard error quiet. Do not redirect standard error into the output file: a diagnostic line before binary pixels would corrupt the PNM stream.

4. Use standard input when a pipeline needs it

If the BMP is being supplied by another command, 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 the file, but it is useful when the BMP comes from a controlled pipeline. Check the producer's exit status if the pipeline is part of a script. In Bash, enable pipeline failure reporting before running the pipeline:

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

There is no privilege requirement in these examples. Only add sudo if the input directory genuinely denies your user access. 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. Recognise failures and recover

Some BMP variants are outside this converter's support. It understands RLE4 and RLE8 compression unless the encoding contains a delta, but it cannot convert BMP files using JPEG or PNG compression. It recognises those cases and reports an error. It also reports an error for OS/2 BMP files with 16 bits per pixel.

When conversion fails, do not use a partial output file. Write to a new temporary name, inspect 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

The mv replaces an existing destination, so use a fresh destination or check it first if preserving the old file matters. If you already created a bad output, remove only that generated file, not the original BMP. A failed conversion does not change the source.

Version history can explain differences between older systems. Support for Windows Version 4 and Version 5 BMP images arrived before Netpbm 10.75. RLE8 support arrived before 10.32, while reliable RLE4 support requires Netpbm 10.85 or later. Netpbm 11.5.2 is newer than those thresholds, but JPEG- or PNG-compressed BMP input remains unsupported.

Done means

  • bmptopnm --version identifies the local Netpbm installation.
  • The input BMP remains unchanged and the output is a separate file.
  • head -n 3 or file confirms a valid PBM, PGM or PPM header.
  • Any verbose diagnostics went to standard error, not into the binary image.
  • Unsupported compression or bit depth produced a failure that did not leave a file you mistake for a valid conversion.