Home / Alt manpages / pbmtomgr(1)

  • pbmtomgr(1)
  • User command
  • linux

Convert a PBM Image to MGR Bitmap with pbmtomgr

You will finish with an MGR bitmap produced from a PBM image, plus a safe way to check that the binary output was written. The examples use Netpbm 11.5.2 from the installed netpbm package. Allow about ten minutes if you already have a PBM file; the only prerequisite is a shell and a readable input image.

pbmtomgr is a small format converter. It reads one PBM image and writes an MGR bitmap to standard output. It does not display the image, choose a destination file, or change system configuration. That output design is useful in pipelines, but it also means that shell redirection is part of the operation and deserves attention.

1. Check the installed command

Start with ordinary, read-only checks. No elevated privileges are needed:

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

The exact build details can differ between distributions. The installed command accepts Netpbm's common -version option. The pbmtomgr-specific manual defines no conversion options.

Checkpoint

If command -v finds nothing, stop and install or enable the Netpbm package through your normal system-management process. Do not download an unrelated converter under the same name.

2. Confirm that the input is PBM

PBM is a monochrome Netpbm format. A raw PBM normally starts with P4; a plain-text PBM starts with P1. The file name is only a convention, so inspect the content before converting it:

$ file /path/to/input.pbm
/path/to/input.pbm: Netpbm image data, size 8 x 4, rawbits, bitmap
$ test -r /path/to/input.pbm && echo 'input is readable'
input is readable

Replace /path/to/input.pbm with your real path. Do not use a JPEG, PNG or greyscale PGM file just because it has been given a .pbm suffix. Convert that source to PBM first with an appropriate Netpbm tool, then check the result.

3. Convert to a new MGR file

Redirect standard output to a new destination. This example keeps the source untouched:

$ pbmtomgr /path/to/input.pbm > /path/to/output.mgr
$ printf 'exit status: %s\n' "$?"
exit status: 0

A status of zero means the converter completed successfully. MGR is a binary bitmap format, so do not open output.mgr in a text editor and do not expect readable text in a terminal. The output is not a portable PBM image and cannot be inspected with PBM tools unless you convert it back.

Safety warning

Shell redirection truncates an existing destination before pbmtomgr starts. If /path/to/output.mgr already contains a useful bitmap, choose another name or make a deliberate backup first. A safer replacement pattern is:

$ pbmtomgr /path/to/input.pbm > /path/to/output.mgr.new
$ test -s /path/to/output.mgr.new
$ mv /path/to/output.mgr.new /path/to/output.mgr

The final mv changes the destination only after a non-empty output file exists. If conversion fails, leave the original file in place and inspect the error; remove the incomplete .new file only after checking that it is the temporary file you intended to discard.

4. Verify the binary result

For a quick smoke test, check that the file exists and contains bytes:

$ stat -c '%n: %s bytes' /path/to/output.mgr
/path/to/output.mgr: 12 bytes
$ xxd -g1 -l32 /path/to/output.mgr
00000000: 79 7a 20 28 20 24 21 20 66 99 3c c3              yz ( $! f.<.

The size and byte values depend on the input dimensions and pixels. Do not compare them with this small example unless your PBM is identical. The useful checks are a successful exit status and a non-empty output file. A successful write does not prove that the source had the picture you intended, so inspect the image with an MGR-aware reader or use the reverse converter if it is installed.

5. Use standard input in a pipeline

The input file argument is optional. With no argument, pbmtomgr reads PBM data from standard input, while the MGR bitmap still goes to standard output:

$ pnminvert /path/to/input.pbm | pbmtomgr > /path/to/inverted.mgr
$ test -s /path/to/inverted.mgr
inverted.mgr

This example assumes pnminvert is installed and deliberately changes the pixels before conversion. If you only want a direct pipeline, use cat /path/to/input.pbm | pbmtomgr > /path/to/output.mgr, although passing the file name is simpler. Keep diagnostics separate from the bitmap: do not redirect standard error into standard output with 2>&1, or text diagnostics could corrupt the binary stream.

When a pipeline fails, check each stage separately. The shell's pipeline status may report only the last command unless your shell is configured with a pipeline-failure option. Start by testing the source and the converter independently:

$ pnminvert /path/to/input.pbm > /tmp/inverted.pbm
$ pbmtomgr /tmp/inverted.pbm > /path/to/inverted.mgr
$ printf 'converter status: %s\n' "$?"
converter status: 0

The temporary PBM in this diagnostic workflow can be discarded after verification. Do not replace your original image during an experiment.

6. Handle common failures

If the input cannot be opened, check its path and permissions as the same user who will run the conversion. If the converter reports an invalid PBM, return to the format check in step 2. A damaged header, a truncated raster or a file in another image format must be repaired or converted before pbmtomgr can use it.

Do not add flags such as width, height, colour or output format: this installed command has no pbmtomgr-specific options. Netpbm's shared options include -quiet, which suppresses informational messages, and -version. Neither changes the MGR format or fixes a bad input. Use pbmtomgr -quiet input.pbm > output.mgr only when you specifically need a quiet standard error stream.

Nothing in this workflow needs sudo unless the input directory or output directory is deliberately restricted. Running the converter as root will not improve the image and can create root-owned output that you cannot conveniently replace later.

Done means

  • The installed command and Netpbm version were checked.
  • The source was confirmed to be a readable PBM image.
  • pbmtomgr returned status 0 and wrote a new, non-empty MGR file.
  • Binary output was kept out of the terminal and standard error was not merged into it.
  • An existing destination was protected with a new temporary name before replacement.
  • No elevated privileges or system configuration changes were needed.