To print an image with ESC/POS, resize it to the printer's dot width (384 dots on most 58 mm printers, 576 on 80 mm), convert it to pure black and white, pack eight dots into each byte, and send it with the raster command GS v 0. QR codes can be printed the same way, or drawn by the printer itself with GS ( k. The hard part isn't the command; it's preparing the picture so it comes out sharp on a 203 dpi, one-colour print head.
Recipe for a sharp receipt logo:
- Flatten transparency onto white.
- Resize to the printable width (or less) with an area-averaging filter.
- Logos and text: threshold. Photos: dither (Floyd–Steinberg or Atkinson).
- Send with
GS v 0, in bands if the image is tall.
Three ESC/POS image commands
| Command | Bytes | When to use it |
|---|---|---|
GS v 0 (raster bit image) | 1D 76 30 m xL xH yL yH + data | Default choice. Supported by nearly every modern receipt printer. Data runs row by row, left to right. |
ESC * (bit image) | 1B 2A m nL nH + data | Older Epson-compatible printers that ignore GS v 0. Data runs in vertical strips of 8 or 24 dots. |
GS ( L (graphics) | 1D 28 4C … | Newer printers. Can store logos in the printer's memory (NV graphics) and print them by key. |
In all three, one bit is one dot: 1 prints black, 0 leaves white, and the most significant bit is the leftmost dot. Our ESC/POS command reference lists the parameters in full.
Step 1: Know your printable width
Most thermal printers print at 203 dpi, or 8 dots per millimetre. The paper is wider than the printable area:
| Paper | Printable width | Dots per line | Bytes per row |
|---|---|---|---|
| 58 mm | 48 mm | 384 | 48 |
| 80 mm | 72 mm | 576 | 72 |
| 80 mm (some models) | 64 mm | 512 | 64 |
Make the image width a multiple of 8, and never wider than the dot width. Too wide, and the printer wraps or drops the extra.
Step 2: Turn the picture into black and white
A thermal head can't print grey. Each dot is either on or off, so you have to decide how to turn shades into dots. This decision makes or breaks the result.
Thresholding: for logos, text and QR codes
Every pixel darker than a cut-off becomes black, the rest white. Edges stay crisp. Pick the cut-off automatically with Otsu's method, or start at 50% grey. Use it for logos, line art, signatures and anything with solid colours.
Dithering: for photos
Dithering spreads the error from each black-or-white decision onto neighbouring pixels, so areas of grey become patterns of dots that look grey from a distance. Floyd–Steinberg gives smooth tones; Atkinson gives higher contrast and often suits thermal paper better. Dithering a logo, though, makes its edges fuzzy. Use it only for photographic images.
Resizing
Resize before converting to black and white, and use an area-averaging filter (Pillow's BOX or LANCZOS). Nearest-neighbour resizing drops thin lines and makes text jagged.
From experience: transparent PNG logos are the most common cause of a solid black rectangle. Transparent pixels often have black colour values with zero alpha. Paste the image onto a white background before you convert it.
Step 3: Build the GS v 0 bytes (Python)
This function takes any image file and returns ready-to-send ESC/POS bytes. It uses Pillow:
# pip install pillow from PIL import Image def raster_image(path, width=384, photo=False): img = Image.open(path) # 1. flatten transparency onto white img = img.convert("RGBA") bg = Image.new("RGBA", img.size, "white") bg.alpha_composite(img) img = bg.convert("L") # 2. resize to the printable width (multiple of 8) width -= width % 8 if img.width > width: img = img.resize((width, round(img.height * width / img.width)), Image.LANCZOS) # 3. black and white: dither photos, threshold logos if photo: img = img.convert("1") # Floyd–Steinberg else: img = img.point(lambda v: 0 if v < 128 else 255, "1") # 4. pack 8 dots per byte, MSB = leftmost, 1 = black w_bytes, h = (img.width + 7) // 8, img.height px, data = img.load(), bytearray() for y in range(h): for xb in range(w_bytes): byte = 0 for bit in range(8): x = xb * 8 + bit if x < img.width and px[x, y] == 0: byte |= 0x80 >> bit data.append(byte) header = bytes([0x1D, 0x76, 0x30, 0x00, w_bytes & 0xFF, w_bytes >> 8, h & 0xFF, h >> 8]) return header + bytes(data) job = b"\x1b@" + b"\x1ba\x01" + raster_image("logo.png") + b"\n"
\x1ba\x01 centres the image. The printer centres a narrower image within the paper.
Print tall images in bands
A full-width image that's a few hundred rows tall can overflow a budget printer's buffer, which shows up as random black noise or a receipt that stops halfway. Split tall images into bands of, say, 128 rows, each with its own GS v 0 header, and send them one after another. If you still see noise, slow the transfer down.
The same in JavaScript
The @point-of-sale/receipt-printer-encoder library (formerly esc-pos-encoder) does the resizing, dithering and encoding for you. Pass an image or canvas:
import ReceiptPrinterEncoder from '@point-of-sale/receipt-printer-encoder'; const img = new Image(); img.src = '/logo.png'; await img.decode(); const bytes = new ReceiptPrinterEncoder({ language: 'esc-pos', columns: 32 }) .initialize() .align('center') .image(img, { width: 320, algorithm: 'threshold' }) // width: multiple of 8 .encode();
Use 'atkinson' or 'floydsteinberg' as the algorithm for photos. More in ESC/POS in JavaScript and Node.js.
Printing QR codes
You have two ways to print a QR code. Each has a place.
Option A: the printer's own QR command
Most current receipt printers draw QR codes themselves with GS ( k. You send only the text, so the job is tiny and the code is perfectly sharp:
def qr(data: bytes, size=6, ec=49): # ec 48=L 49=M 50=Q 51=H n = len(data) + 3 return (b"\x1d(k\x04\x00\x31\x41\x32\x00" # model 2 + b"\x1d(k\x03\x00\x31\x43" + bytes([size]) # module size + b"\x1d(k\x03\x00\x31\x45" + bytes([ec]) # error correction + b"\x1d(k" + bytes([n & 0xFF, n >> 8]) + b"\x31\x50\x30" + data + b"\x1d(k\x03\x00\x31\x51\x30") # print job = b"\x1b@\x1ba\x01" + qr(b"https://example.com/r/1042") + b"\n"
A module size of 5–8 dots suits receipts; bigger modules scan more easily from a phone held at arm's length.
Option B: print the QR code as an image
Some cheap printers ignore GS ( k and print nothing. Payment QR codes (UPI, QRIS, PromptPay, Pix) usually arrive from your payment provider as an image anyway. In both cases, generate or load the QR image and print it with GS v 0, using a threshold, never dithering. Scale it by whole numbers (each module exactly 4, 5 or 6 dots wide) so every module stays the same size.
Changing font size
Text size uses GS ! n, where the high four bits set width (multiplier − 1) and the low four bits set height:
1D 21 00: normal1D 21 01: double height1D 21 10: double width1D 21 11: double width and height, good for totals and order numbers1D 21 22: triple size
ESC M 1 switches to the smaller Font B for dense lines. For sizes and fonts the printer can't do (or scripts it doesn't have), render the text into an image and print that.
Printing from Android? Raw Print does all of the above for you: area-average scaling, Floyd–Steinberg dithering for photos, an automatic threshold for logos, transparent PNGs flattened onto white, and a choice of GS v 0 or ESC * if your printer needs it. A web page can send it a PNG directly with rawbt:data:image/png;base64,….
Troubleshooting images
- Solid black rectangle: transparency wasn't flattened onto white, or black and white are inverted (1 must mean black).
- Random noise or garbage text after the image: the printer buffer overflowed, or the header's width/height bytes are wrong. Print in bands and check that the byte count equals bytes-per-row × rows.
- Image stretched or squashed: you used a double-width or double-height mode (
m= 1, 2 or 3), or calculated the height wrongly when resizing. - Nothing prints: the printer doesn't support
GS v 0. TryESC *in 24-dot mode. - Grey, washed-out photo: raise the print darkness or use Atkinson dithering for more contrast.
- QR code won't scan: increase the module size, use error correction M or Q, and don't dither QR images.
Test image code without wasting paper using an ESC/POS emulator.
Frequently asked questions
What image size should I use for a 58 mm thermal printer?
At most 384 dots wide, a multiple of 8. For a logo, 200–320 dots wide usually looks best and prints faster.
Can a thermal printer print colour or greyscale images?
No. Each dot is black or white. Dithering simulates grey with patterns of dots, which works well for photos.
Which is better, GS v 0 or ESC *?
GS v 0 is simpler and supported by most modern printers. Use ESC * only for printers that ignore GS v 0.
Why does my QR code print with GS ( k on one printer but not another?
Some budget printers don't implement the 2D barcode commands. Print the QR code as an image on those printers.