diff options
Diffstat (limited to 'tools/barcode_ref.py')
| -rw-r--r-- | tools/barcode_ref.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/tools/barcode_ref.py b/tools/barcode_ref.py index 2e58b5e..f56a7c3 100644 --- a/tools/barcode_ref.py +++ b/tools/barcode_ref.py @@ -16,6 +16,7 @@ from PIL import Image QR, CODE128, CODE39, EAN13, EAN8, UPCA, UPCE, ITF, CODABAR = range(9) FORMATS = { + QR: zxingcpp.BarcodeFormat.QRCode, CODE128: zxingcpp.BarcodeFormat.Code128, CODE39: zxingcpp.BarcodeFormat.Code39, EAN13: zxingcpp.BarcodeFormat.EAN13, @@ -54,6 +55,18 @@ def render_linear(modules: str, scale: int, height: int = 40) -> Image.Image: READ_FORMATS = {**FORMATS, CODE39: zxingcpp.BarcodeFormat.Code39Std, UPCA: zxingcpp.BarcodeFormat.EAN13} +def render_matrix(packed: bytes, size: int, scale: int, quiet: int) -> Image.Image: + """A square matrix (row-major bits, 8 per byte) as black modules on white with a quiet zone.""" + side = (size + 2 * quiet) * scale + image = Image.new("L", (side, side), 255) + for i in range(size * size): + if packed[i // 8] & (0x80 >> (i % 8)): + x, y = i % size, i // size + left, top = (quiet + x) * scale, (quiet + y) * scale + image.paste(0, (left, top, left + scale, top + scale)) + return image + + def decode(image: Image.Image, symbology: int | None = None) -> list[str]: """Texts zxing-cpp reads in image (only as `symbology` when given).""" if symbology is None: |
