Images, barcodes and labels with Jennifer

Jennifer

Pictures with images and ImageMagick™, barcodes you can scan, labels a printer understands, a scanner that hands you a PDF.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 21

The toolbox

NameWhatRuns on
use images;library: PNG, JPEG, GIF in, edit, outboth binaries
@deckyard/magickdeck: ImageMagick 7, needs execjennifer
import "barcode.j"module: QR, DataMatrix, 1D codes as SVG or PNGboth
import "label.j"module: ZPL and cab printer commandsboth; send needs jennifer
@deckyard/escldeck: network scanners (AirScan, Mopria™), needs netjennifer
import "pdf.j"module: the PDF writerboth
2 / 21

Load, look, shrink

def data as bytes init fs.readBytes("holiday.jpg");
io.printf("%v\n", images.info($data)); # the header only
def photo as images.Image init images.decode($data);
def thumb as images.Image init images.fit($photo, 320, 320);
io.printf("%v\n", $thumb);
fs.writeBytes("thumb.jpg", images.encode("jpeg", $thumb, 85));
fs.writeBytes("thumb.png", images.encode("png", $thumb));
images.Info{format: "jpeg", width: 3000, height: 2000}
<images.Image 320x213>
3 / 21

Crop, resize, turn, dither

def sq as images.Image init images.crop($img, 1000, 1000, 1000, 1000);
def small as images.Image init images.resize($sq, 200, 0);
def gray as images.Image init images.grayscale($small);
def dots as images.Image init images.dither($gray);
def turned as images.Image init images.rotate($dots, 90);
$strip = images.draw($strip, $small, 0, 0); # ... and the other three

Cropped, grayscale, dithered, rotated

4 / 21

Hostile pictures

def data as bytes init fs.readBytes("bomb.png"); # 75 bytes
io.printf("%v\n", images.info($data));
try {
    images.decode($data, 4000000); # 4 Mpx, for uploads
} catch (e) {
    if ($e.kind != "limit") {
        throw $e;
    }
    io.printf("%s\n", $e.message);
}
images.Info{format: "png", width: 100000, height: 100000}
images.decode: a 100000x100000 image exceeds the 4000000-pixel limit
5 / 21

What images does not do: @deckyard/magick

import "@deckyard/magick/" as magick;
def c as magick.Config init magick.config();
def info as magick.Info init magick.identify($c, "phone.jpg");
io.printf("%dx%d %s\n", $info.width, $info.height, $info.orientation);
def img as magick.Image init magick.open($c, "phone.jpg");
$img = magick.thumbnail(magick.autoOrient($img), 640, 640);
$img = magick.annotate($img, "Summer 2026", "SouthEast", 12, 8, 28,
    "white");
magick.write($c, magick.quality($img, 80), "phone.webp");
$info = magick.identify($c, "phone.webp");
io.printf("%s %dx%d\n", $info.format, $info.width, $info.height);
1067x1600 LeftBottom
WEBP 640x427
6 / 21

Barcodes: one Symbol, several renderers

import "barcode.j" as barcode;
def opts as barcode.Options init barcode.defaults();
$opts.scale = 4;
for (def ec in ["L", "M", "Q", "H"]) {
    $opts.ecLevel = $ec;
    def qr as barcode.Symbol init barcode.encode("qr",
        "https://academy.jennifer-lang.dev/", $opts);
    def png as bytes init barcode.png($qr, $opts);
    fs.writeBytes("qr-{$ec}.png", $png);
    io.printf("%s: %d modules, %d bytes\n", $ec, $qr.size, len($png));
}
L: 29 modules, 1347 bytes      Q: 33 modules, 1756 bytes
M: 29 modules, 1325 bytes      H: 33 modules, 1715 bytes
7 / 21

Error correction, tested with a sticker

def hit as images.Image init images.fill($m, 3 * $q, 3 * $q, 2 * $q,
    2 * $q, $red); # a sticker in the middle

The four QR codes with a red square over the middle

$ for l in L M Q H; do echo $l: $(zbarimg -q hit-$l.png || echo none); done
L: none
M: none
Q: QR-Code:https://academy.jennifer-lang.dev/
H: QR-Code:https://academy.jennifer-lang.dev/
8 / 21

Four more symbologies

def o as barcode.Options init barcode.defaults();
$o.scale = 3;
def jobs as list of list of string init [
    ["code128", "JN-2026-0042"],
    ["ean13", "400638133393"],
    ["gs1-128", "(01)09012345678901(10)LOT42"],
    ["datamatrix", "JN-2026-0042"]
];
for (def job in $jobs) {
    def s as barcode.Symbol init barcode.encode($job[0], $job[1], $o);
    fs.writeBytes($job[0] + ".png", barcode.png($s, $o));
    fs.writeString($job[0] + ".svg", barcode.svg($s, $o));
}
9 / 21

Scan them back

Code 128, EAN-13 and DataMatrix, from the SVG and PNG files

$ zbarimg -q code128.png ean13.png gs1-128.png
CODE-128:JN-2026-0042
EAN-13:4006381333931
CODE-128:010901234567890110LOT42
$ ZXingReader datamatrix.png | head -1
Text:       "JN-2026-0042"
10 / 21

SVG or PNG

barcode.svgbarcode.png
Scaleswithout loss: web, email, PDF via a converterin whole pixels: scale per module
Coloursforeground, backgroundblack on white
Text line under 1D codesyes (humanReadable)no
Code 128 above, bytes3,059557
11 / 21

Labels: build, render, emit

def t as label.TextOptions;
$t.height = 5.0;
def b as label.BarcodeOptions;
$b.height = 10.0;
def l as label.Label init label.new(50.0, 30.0); # millimetres
$l = label.box($l, 1.0, 1.0, 48.0, 28.0, 0.4);
$l = label.text($l, 4.0, 4.0, $t, "Shelf A-17");
$l = label.barcode($l, "code128", 4.0, 12.0, $b, "A17-0042");
$l = label.quantity($l, 2);
io.printf("%s\n", label.render($l, label.zpl(203)));
io.printf("%s", label.render($l, label.cab()));
12 / 21

The same label, twice

^XA
^FO8,8^GB384,224,3^FS
^FO32,32^A0N,40,40^FH^FDShelf A-17^FS
^FO32,96^BY2^BCN,80,Y,N,N^FH^FDA17-0042^FS
^PQ2
^XZ
m m
J
S 0,0,30.0,30.0,50.0
G 1.0,1.0,0;R:48.0,28.0,0.4,0.4
T 4.0,4.0,0,3,5.0;Shelf A-17
B 4.0,12.0,0,CODE128,10.0,0.3;A17-0042
A 2
13 / 21

Pictures on labels, and sending

def l as label.Label init label.new(50.0, 30.0);
$l = label.image($l, 2.0, 2.0, "R:LOGO.GRF"); # stored on the printer
def zpl as string init label.render($l, label.zpl(300));
label.send("127.0.0.1", 9100, $zpl); # the raw port; here a stand-in
io.printf("sent %d bytes\n", len($zpl));
sent 42 bytes
42 bytes:
^XA
^FO24,24^XGR:LOGO.GRF,1,1^FS
^PQ1
^XZ
14 / 21

A scanned page as a PDF: @deckyard/escl

def st as escl.Settings init escl.settings(); # platen, JPEG
$st.colorMode = "Grayscale8";
$st.resolution = 150;
def s as escl.Scanner init escl.scanner(os.ARGS[1]); # host:port
def pages as list of escl.Page init escl.scan($s, $st);
for (def i in 0..len($pages)) {
    def img as images.Image init images.decode($pages[$i].data);
    def bw as images.Image init images.threshold($img, 160);
    $pages[$i].data = images.encode("png", $bw);
    $pages[$i].contentType = "image/png";
}
fs.writeBytes("letter.pdf", scanpdf.build($pages, $st.resolution));
15 / 21

What came back

The top of the scanned delivery note, as bound into the PDF

fake scanner: GET /eSCL/ScannerCapabilities
fake scanner: POST /eSCL/ScanJobs
fake scanner: GET /eSCL/ScanJobs/7/NextDocument
fake scanner: GET /eSCL/ScannerStatus
fake scanner: DELETE /eSCL/ScanJobs/7

A 507,190 byte JPEG page became a 20,358 byte PDF.

16 / 21

Worked example: the shipping label in ZPL

func zplLabel(s as Shipment) {
    # big: TextOptions, 6 mm; bar: 20 mm high; qr: errorLevel "Q"
    def l as label.Label init label.new(100.0, 150.0);
    $l = label.box($l, 3.0, 12.0, 94.0, 50.0, 0.5);
    def y as float init 16.0;
    for (def line in $s.lines) {
        $l = label.text($l, 7.0, $y, $big, $line);
        $y = $y + 10.0;
    }
    $l = label.barcode($l, "code128", 7.0, 70.0, $bar, $s.tracking);
    return label.barcode($l, "qr", 60.0, 105.0, $qr, $s.url);
}
17 / 21

The same shipment as a PDF

func embed(name as string, kind as string, data as string, n as int) {
    def o as barcode.Options init barcode.defaults();
    $o.scale = $n; # pixels (= points) per module
    $o.height = 50; # bar height in modules
    $o.ecLevel = "Q";
    def sym as barcode.Symbol init barcode.encode($kind, $data, $o);
    return pdf.loadImage($name, barcode.png($sym, $o));
}
# in pdfLabel: a 283 x 425 point page, the address with pdf.text
# after pdf.toWinAnsi, then the two pictures:
$pg = pdf.drawImage($pg, $bc, 10, 170, $bc.width * 3 // 2, 50);
$pg = pdf.drawImage($pg, $qr, 150, 20, $qr.width, $qr.height);
18 / 21

The result

The shipping label PDF at 50 dpi

$ pdftoppm -r 150 -png ship.pdf ship && zbarimg -q ship-1.png
QR-Code:https://example.com/t/JN00004200AT
CODE-128:JN00004200AT

^FO56,208^A0N,48,48^FH^FDMusterstra_C3_9Fe 12^FS

19 / 21

Errors and limits

WhereKindReal message
images"limit"a 100000x100000 image exceeds the 4000000-pixel limit
images"runtime"degrees must be a multiple of 90, got 45
magick"magick", "timeout"the format SVG is not allowed; allowFormat enables it
barcode"barcode"ean: check digit mismatch (last digit does not verify)
label"label"ean13 barcode data must be numeric: 40063813339^XZ
escl"escl" (EsclError)200 dpi not offered; the scanner has 150, 300

Match the kind, never the message.

20 / 21

Read more

Based on Jennifer 0.27.0, November 2026.

21 / 21