Convert PBM Images to Epson Printer Streams with pbmtoepson
You will finish with a repeatable way to convert a PBM bitmap into Epson printer control data, inspect the result, and only then decide whether it is safe to send that data to a printer. The examples use Netpbm 11.5.2 from package netpbm 2:11.05.02-1.1build1 on this machine.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about fifteen minutes. You need a readable PBM file and the Netpbm tools. A printer is optional for this guide. Keep the first output in an ordinary file: printer streams are binary control data, not an image format you can sensibly inspect in a text editor.
1. Check the installed command
Confirm that the command is installed and record the version before relying on examples. These are read-only commands and do not need elevated privileges:
$ command -v pbmtoepson
/usr/bin/pbmtoepson
$ pbmtoepson --version
pbmtoepson: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
...
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
The installed manual describes four command-specific controls: -protocol=escp9 or -protocol=escp, -dpi=n, -adjacent, and -nonadjacent. Netpbm also accepts common libnetpbm options such as -quiet. Option names use a single hyphen, as shown.
Checkpoint
Stop here if command -v finds nothing. Install Netpbm through your normal package-management process, then repeat this check. Do not work around a missing executable by guessing its location.
2. Prepare a small PBM test
Use a tiny monochrome PBM when checking the pipeline. The following is portable plain PBM syntax, so it can be supplied through standard input without creating a source file:
$ printf 'P1\n8 8\n0 0 1 1 1 1 0 0\n0 1 0 0 0 0 1 0\n1 0 1 0 0 1 0 1\n1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1\n1 0 1 0 0 1 0 1\n0 1 0 0 0 0 1 0\n0 0 1 1 1 1 0 0\n' > /tmp/test-pattern.pbm
This creates temporary test data under /tmp. For a real image, replace that path with the PBM file you already have. Do not assume that a file with a bitmap-looking extension is PBM; use the file command if you are unsure:
$ file /path/to/input.pbm
/path/to/input.pbm: Netpbm image data, size 8 x 8, ASCII bitmap
The exact description varies with the PBM variant. The important check is that the file is identified as Netpbm bitmap data and is readable.
3. Convert to a file first
Run the converter with explicit settings and redirect standard output to a file. This example selects the newer ESC/P protocol and 60 dots per inch:
$ pbmtoepson -protocol=escp -dpi=60 /tmp/test-pattern.pbm > /tmp/test-pattern.escp
$ printf 'exit status: %s\n' "$?"
exit status: 0
$ wc -c /tmp/test-pattern.escp
20 /tmp/test-pattern.escp
pbmtoepson reads the named PBM file and writes Epson graphics to standard output. If you omit the filename, it reads standard input instead. The output is not a PBM, PDF or ordinary raster file. It is a printer stream containing control bytes and image data.
Safety boundary
Do not redirect an untested command straight to a device such as /dev/usb/lp0. That can print immediately, consume paper and alter printer state. The file conversion above is ordinary user work. Accessing a printer device may require membership of a device group or elevated privileges, but sudo does not make an incompatible protocol safe.
4. Inspect and verify the stream
Check that the output is non-empty and begins with plausible Epson control bytes. Do not treat this as a complete printer compatibility test:
$ xxd -g1 -l 32 /tmp/test-pattern.escp
00000000: 1b 41 08 1b 2a 00 08 00 3c 5a bd 99 99 bd 5a 3c .A..*...<Z....Z<
00000010: 0a 0c 1b 40 ...@
$ test -s /tmp/test-pattern.escp && echo 'stream is non-empty'
stream is non-empty
Your bytes will change with the input image, protocol, density and dot-printing mode. The command's zero exit status means the conversion completed; it does not prove that a particular Epson model accepts the selected stream.
If you need to pass a PBM through a pipeline, the equivalent form is:
$ pbmtoepson -protocol=escp -dpi=60 < /path/to/input.pbm > /tmp/output.escp
Using standard input is useful in scripts, but it makes the source less obvious when you return to the command later. Prefer a named input file while diagnosing a failed conversion.
5. Choose protocol and density deliberately
Use -protocol=escp9 for the older ESC/P 9-pin protocol or -protocol=escp for the newer ESC/P protocol. The manual points modern ESC/P2 printers to the separate pbmtoescp2 program. Do not select escp merely because the printer is an Epson; check the model's protocol documentation.
The accepted -dpi values are exactly 60, 72, 80, 90, 120, 144 and 240. It controls horizontal print density. For example:
$ pbmtoepson -protocol=escp9 -dpi=120 /path/to/input.pbm > /tmp/output-escp9.120dpi
If you omit -dpi, the program chooses a density consistent with the other options. That default is convenient, but explicit values make scripted output easier to reproduce. An unsupported value fails before useful output is produced:
$ pbmtoepson -dpi=61 /path/to/input.pbm > /tmp/invalid.escp
pbmtoepson: Invalid DPI value: 61. This program knows only 60, 72, 80, 90, 120, 144, and 240.
For a first test, leave -adjacent and -nonadjacent unset. By default, the program selects adjacent dot printing unless that conflicts with the other choices. Add one of these options only when the printer or the appearance of the output gives you a reason to control it. Do not use both together.
6. Recover from common failures
A non-zero exit status usually points to an unreadable or invalid PBM, an unsupported density, or an invalid option. Read the diagnostic before changing permissions or using sudo:
$ test -r /path/to/input.pbm && echo readable
$ pbmtoepson -dpi=240 /path/to/input.pbm > /tmp/output.escp
$ printf 'exit status: %s\n' "$?"
If the output is wrong for the printer, keep the PBM and remove only the generated stream after checking that no process is using it:
$ rm /tmp/output.escp
This removes a derived temporary file, not the source image. Do not remove the original PBM as a way to fix a conversion problem. If you already sent a stream to a printer, stop the job using that printer's normal queue controls; pbmtoepson itself has no reverse-conversion command and cannot retract paper already printed.
Done means
- You confirmed the installed Netpbm version and the PBM input type.
- You converted to a file before considering printer output.
- You used a supported protocol and, where reproducibility mattered, an explicit density.
- You checked the exit status and confirmed that the stream is non-empty.
- You matched ESC/P, ESC/P 9-pin or ESC/P2 to the printer's documentation.
- You kept the PBM source intact and know how to remove only a disposable derived stream.