Home / Alt manpages / ppmtobmp(1)

  • ppmtobmp(1)
  • User command
  • linux

Convert PPM Images to BMP Without Losing Control of Colour Depth

You will finish with a repeatable way to turn a PPM image into a BMP file, select Windows or OS/2 format, and check what was actually written. The examples use Netpbm 11.5.2, installed here as Debian package version 2:11.05.02-1.1build1.

Allow about fifteen minutes. You need a shell, ppmtobmp, a readable PPM file, and permission to write the destination directory. Conversion normally needs no elevated privileges. Do not use sudo unless the input or output location is deliberately restricted, and check the ownership problem before changing permissions.

1. Check the installed converter

Confirm that the shell will run the Netpbm program you expect. This is a read-only checkpoint:

$ command -v ppmtobmp
/usr/bin/ppmtobmp
$ ppmtobmp --version
ppmtobmp: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
ppmtobmp: Built from source dated 2024-03-31 09:09:47

The version text has extra build details on this installation. The useful confirmation is the Netpbm version. The installed manual says that ppmtobmp reads PPM from the named file, or from standard input when no file is supplied, and writes the BMP stream to standard output.

2. Convert one PPM to the default BMP format

Choose a new output name and redirect the binary output there:

$ ppmtobmp /path/to/input.ppm > /path/to/output.bmp

Windows BMP is the default. The command chooses the smallest supported bit depth that represents every colour in the input. The supported values are 1, 4, 8 and 24 bits per pixel. A small image might therefore become a paletted 4-bit BMP rather than a 24-bit true-colour file.

Checkpoint: inspect the file without opening it in a text editor:

$ file /path/to/output.bmp
/path/to/output.bmp: PC bitmap, Windows 3.x format, 2 x 2 x 4, cbSize 126, bits offset 118

Your dimensions and exact file wording will differ. Look for a BMP description, the expected dimensions, and a sensible bit depth. A successful shell return status only says that the command completed; it does not prove that the image looks right.

3. Request a specific bit depth

Use -bpp when another program or an interchange requirement needs a known representation. This example requests 24-bit Windows BMP:

$ ppmtobmp -bpp 24 /path/to/input.ppm > /path/to/output-24bit.bmp
$ file /path/to/output-24bit.bmp
/path/to/output-24bit.bmp: PC bitmap, Windows 3.x format, 2 x 2 x 24, cbSize 70, bits offset 54

The option also accepts the equals form, -bpp=24. Do not request 1, 4 or 8 bits merely to make a smaller file. If the input contains more distinct colours than that depth can represent, ppmtobmp stops and suggests reducing the image first with pnmquant or ppmdither.

For example, this deliberately fails on an image with four colours:

$ ppmtobmp -bpp 1 /path/to/input.ppm > /path/to/too-small.bmp
ppmtobmp: There are too many colors in the image to represent in the number of bits per pixel you requested: 1.  You may use Pnmquant to reduce the number of colors in the image.

The diagnostic on this Netpbm build uses the American spelling inside the program's own message. That is upstream output, not a recommendation for prose. The redirected file may be empty or incomplete after a failed conversion, so do not treat it as a usable BMP.

4. Select OS/2 BMP when a consumer requires it

Use -os2 for an OS/2 BMP. Windows BMP remains the default, so include this option explicitly in scripts that must produce the older format:

$ ppmtobmp -os2 /path/to/input.ppm > /path/to/output-os2.bmp
$ file /path/to/output-os2.bmp
/path/to/output-os2.bmp: PC bitmap, OS/2 1.x format, 2 x 2 x 4, cbSize 82, bits offset 74

-windows is the explicit spelling for the default Windows format. It is useful when a command line is generated or reviewed and the output format must be visible:

$ ppmtobmp -windows /path/to/input.ppm > /path/to/output-windows.bmp

Do not infer compatibility from the extension alone. Verify the header with file or the receiving program's own inspection tools.

5. Protect an existing output

Shell redirection with > truncates an existing destination before ppmtobmp starts. That is an irreversible overwrite if the old file has no backup. Use a temporary name, then replace the destination only after verification:

$ ppmtobmp /path/to/input.ppm > /path/to/output.bmp.new
$ file /path/to/output.bmp.new
/path/to/output.bmp.new: PC bitmap, Windows 3.x format, 2 x 2 x 4, cbSize 126, bits offset 118
$ mv /path/to/output.bmp.new /path/to/output.bmp

The final mv changes state and can replace the old file. Run it only when the temporary file is non-empty and its format and dimensions are correct. If conversion fails, leave the original output alone and remove the failed temporary file after checking its path. If you replaced a file and need to undo it, restore your own backup, for example with cp --preserve=all /path/to/output.bmp.bak /path/to/output.bmp.

6. Keep the input colour range in mind

For faithful colour reproduction, the PPM maxval should be 255. If it is another value, the manual warns that the BMP colours may be slightly different. Check the PPM header before blaming the converter or the viewer:

$ sed -n '1,3p' /path/to/input.ppm
P3
2 2
255

This only reads the first three header lines for a simple plain-text PPM. Comments and a binary PPM layout can make line-based inspection misleading, so use a Netpbm-aware tool when the header is not simple. Do not edit binary image data in a text editor.

7. Use a supplied palette only when indices matter

-mapfile lets you control the order of palette entries. It does not recolour the image. The map file is a Netpbm image with one distinct pixel per palette entry, with entries read from top to bottom and left to right, and it may contain no more than 256 pixels.

A grayscale workflow from the manual creates a palette whose index matches the grey value:

$ pamseq 1 255 > mapfile.pgm
$ ppmtobmp -mapfile=mapfile.pgm input.pgm > output.bmp

The supplied palette must contain every colour present in the input. If it does not, the conversion reports the missing colour and the output is not suitable for use. Use pnmremap first when the input needs to be adjusted to a chosen palette. This option is for predictable palette indices, not for reducing arbitrary colour counts.

Done means

  • ppmtobmp --version confirmed the installed Netpbm build.
  • The output format was chosen explicitly when a Windows or OS/2 distinction mattered.
  • The bit depth was left automatic or requested with a verified supported value.
  • file confirmed that the result is a BMP with expected dimensions and depth.
  • An existing destination was protected from truncation until the replacement was checked.
  • A custom palette was used only when its entries covered the input colours and its ordering mattered.