Send a PBM Image to an ESC/P2 Printer with pbmtoescp2
You will turn a PBM bitmap into an ESC/P2 printer control stream, save it without mixing binary data into your terminal, and check the settings before sending it to an Epson printer. Allow about fifteen minutes for a first conversion, plus time to confirm that the image dimensions suit the paper.
The route
Jump straight to the step you need, or tick off Done means at the end.
This guide uses the pbmtoescp2 shipped in Netpbm package version 2:11.05.02-1.1build1. You need a PBM file and an Epson printer that understands ESC/P2. The command does not speak to the printer itself: it writes a binary stream to standard output, which you can save or pass to a separate printing workflow.
1. Check the installed command
Confirm that the executable is available. This is an ordinary read-only check and does not need elevated privileges:
$ command -v pbmtoescp2
/usr/bin/pbmtoescp2
$ dpkg-query -W -f='\${Package} \${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
The installed help output points to the manual page rather than listing options. Read that page when checking a host with a different Netpbm version:
$ pbmtoescp2 --help
pbmtoescp2: Use 'man pbmtoescp2' for help.
Checkpoint: if command -v finds nothing, stop and install Netpbm through your normal package-management process. Do not work around a missing command by copying a binary from an unrelated host.
2. Check the PBM dimensions
Each input pixel becomes one printed dot. PBM has no inherent resolution, so the -resolution option describes how densely those pixels should be printed. Inspect the image before choosing settings:
$ file /path/to/image.pbm
/path/to/image.pbm: Netpbm image data, size 1440 x 1800, bitmap, ASCII text
$ head -n 3 /path/to/image.pbm
P1
1440 1800
...
For example, a 1440 by 1800 bitmap at 180 dpi represents 8 by 10 inches. The same pixel dimensions at 360 dpi represent 4 by 5 inches. If the size is wrong, resize the PBM with a suitable Netpbm tool before conversion. Do not expect pbmtoescp2 to resize it.
3. Convert a PBM to a printer stream
Save standard output to a new file. The default settings are 360 dpi, RLE compression, and a stripe height of 24 lines:
$ pbmtoescp2 /path/to/image.pbm > /path/to/image-escp2.bin
$ wc -c /path/to/image-escp2.bin
123456 /path/to/image-escp2.bin
The byte count will depend on the image and compression. It should be greater than zero. Do not run the command without redirection on an interactive terminal: the result is binary control data, not readable text.
Input can also come from standard input. This is useful in a pipeline, but keep the output redirection visible so that the stream is not lost:
$ cat /path/to/image.pbm | pbmtoescp2 > /path/to/image-escp2.bin
$ test -s /path/to/image-escp2.bin && echo 'non-empty ESC/P2 stream'
non-empty ESC/P2 stream
There is no need for sudo when reading an image and writing in a directory you own. Use elevated privileges only if a later, separate printing command genuinely requires access to a protected device or spooler.
4. Choose resolution and compression deliberately
The valid resolutions are 180, 360 and 720 dpi. The value sets both horizontal and vertical resolution in the printer stream. The default of 360 is not a measurement read from the PBM:
$ pbmtoescp2 -resolution=180 /path/to/image.pbm > /path/to/image-180dpi.bin
$ wc -c /path/to/image-180dpi.bin
123456 /path/to/image-180dpi.bin
RLE compression is enabled by default with -compress=1 and is usually effective for line drawings, text and other repeated horizontal patterns. A photograph-like bitmap may compress poorly. In that case, use uncompressed raster data with -compress=0 and compare the resulting file size:
$ pbmtoescp2 -compress=0 -resolution=180 /path/to/image.pbm > /path/to/image-180dpi-raw.bin
$ wc -c /path/to/image-180dpi-raw.bin
234567 /path/to/image-180dpi-raw.bin
The numbers above are examples of the output shape, not expected sizes. Keep the smaller stream only if the printer accepts that mode and the printed result is correct.
5. Adjust stripe handling when the printer needs it
Raster data is emitted in vertical stripes. The default stripe height is 24 lines. The printer language has standard heights of 1, 8 and 24, although the command accepts values from 1 to 255 and warns for other values:
$ pbmtoescp2 -stripeheight=8 /path/to/image.pbm > /path/to/image-stripe8.bin
$ test -s /path/to/image-stripe8.bin && echo 'stripe-8 stream written'
stripe-8 stream written
Use this option for a documented printer or integration requirement, not as a general quality setting. A stripe height of 8 does not change the input image dimensions. The output is padded vertically to the stripe boundary and horizontally to a multiple of eight columns. On current Netpbm releases, such padding preserves the image; releases before 10.72 truncated at those boundaries.
6. Add a form feed or generate raw blocks only
-formfeed adds a form-feed command at the end of the output. Use it when the receiving printer workflow expects the stream to finish the page:
$ pbmtoescp2 -formfeed /path/to/image.pbm > /path/to/image-with-formfeed.bin
-raw has a different purpose. It omits the printer setup commands and generates only data blocks, so it is suitable for inserting raster data into a larger command stream that already establishes the printer state. Do not use it as the normal standalone conversion:
$ pbmtoescp2 -raw /path/to/image.pbm > /path/to/image-data-blocks.bin
These options change the protocol stream, not the PBM. If a complete standalone file is required, leave -raw out.
7. Check failures before retrying
An invalid resolution is rejected rather than guessed:
$ pbmtoescp2 -resolution=300 /path/to/image.pbm > /tmp/should-not-be-used.bin
pbmtoescp2: Invalid -resolution value: 300. Only 180, 360 and 720 are valid.
Check the exit status immediately after a conversion, especially in a script:
if pbmtoescp2 -resolution=360 /path/to/image.pbm > /path/to/image-escp2.bin; then
test -s /path/to/image-escp2.bin || exit 1
printf '%s\n' 'ESC/P2 stream ready'
else
status=$?
printf 'conversion failed with status %s\n' "$status" >&2
exit "$status"
fi
Shell redirection truncates an existing destination before the program starts. To protect a useful output, write a temporary file in the same directory and rename it only after success:
tmp='/path/to/image-escp2.bin.new'
pbmtoescp2 /path/to/image.pbm > "$tmp" && test -s "$tmp" && mv -- "$tmp" /path/to/image-escp2.bin
If the conversion fails, the old destination remains in place, but the temporary file may be incomplete and should be removed after checking it. The mv changes the destination, so keep a backup if the existing stream is difficult to recreate.
Done means
- The installed Netpbm version and
pbmtoescp2path were checked. - The PBM dimensions match the intended physical size at the selected dpi.
- A non-empty ESC/P2 stream was written to a file rather than displayed in the terminal.
- Compression, stripe height, form feed and raw mode were selected for a known reason.
- The exit status and output size were checked, and an existing stream was not overwritten accidentally.
- The stream will be sent only to an Epson printer or workflow that supports ESC/P2.