tools / image / qr.py

image/qr.py

imagemedia○ hermeticpython3

Make a QR code (PNG or SVG) for a link or any text, computed locally with segno and no network: what it encodes never leaves the container. Text that is a secret (an invite link, a Wi-Fi password) goes on stdin, not in text=: params are written into every receipt in plain text, stdin only as a hash. Prints the code's version and size, and with show=true the code itself, drawn in the terminal.

Use when the user wants a QR code, a scannable link, or a printable code for a URL, an invite, Wi-Fi details or any short text.

Run it

1. Once per machine: install fragletc (Docker must already be running).

curl -fsSL https://raw.githubusercontent.com/ofthemachine/fraglet/main/install.sh | sh

2. Fetch the tool and print its contract.

curl -fsSLO https://tools.ofthemachine.com/image/qr.py && chmod +x qr.py && ./qr.py --fraglet-help

3. Run it.

./qr.py [-p text="<text>"] [-p format=png] [-p scale=8] [-p border=4] [-p error=M] [-p show=false] --output qr.png:optional

Parameters

nametyperequirementdescription
textstringoptionalWhat the code encodes (or pipe it on stdin, which a secret must use)
formatstringdefault pngpng (raster) or svg (vector)
scalestringdefault 8Pixels per module (the code's smallest square)
borderstringdefault 4Quiet zone around the code, in modules (scanners want 4)
errorstringdefault MError correction: L 7%, M 15%, Q 25%, H 30% of the code may be lost and still scan
showstringdefault falseAlso draw the code on stdout with text blocks, to scan from a terminal
stdinpipeoptionalread in full and hashed into the receipt

Outputs

qr.png:optional, qr.svg:optional

Provenance

image
ofthemachine/python3@sha256:1a6a8e9cd91c93359794245e2a36b883ee54dfef06356ff8be1d4d863ee3823a
reach
network=none — a run is a pure function of its inputs; results are safe to memoize.
procedure_hash
sha256:2082c311e3391775ec7c9bacb86b7c8a6254d044f6861b3b00f524e390c590e2
receipt
add --receipt run.json to any run; receipt/attest.py verifies it against this hash without re-running.

Source

qr.py · 37 lines · sha256 2082c311e339…
#!/usr/bin/env -S fragletc --image ofthemachine/python3@sha256:1a6a8e9cd91c93359794245e2a36b883ee54dfef06356ff8be1d4d863ee3823a
#: d=Make a QR code (PNG or SVG) for a link or any text, computed locally with segno and no network: what it encodes never leaves the container. Text that is a secret (an invite link, a Wi-Fi password) goes on stdin, not in text=: params are written into every receipt in plain text, stdin only as a hash. Prints the code's version and size, and with show=true the code itself, drawn in the terminal.
#: when=Use when the user wants a QR code, a scannable link, or a printable code for a URL, an invite, Wi-Fi details or any short text.
#: network=none
#: stdin=buffer
#: param=text:d=What the code encodes (or pipe it on stdin, which a secret must use)
#: param=format:default=png:kind=enum(png|svg):d=png (raster) or svg (vector)
#: param=scale:default=8:kind=range(1..40):d=Pixels per module (the code's smallest square)
#: param=border:default=4:kind=range(0..10):d=Quiet zone around the code, in modules (scanners want 4)
#: param=error:default=M:kind=enum(L|M|Q|H):d=Error correction: L 7%, M 15%, Q 25%, H 30% of the code may be lost and still scan
#: param=show:default=false:kind=bool:d=Also draw the code on stdout with text blocks, to scan from a terminal
#: output=qr.png:optional
#: output=qr.svg:optional
import os
import sys

import segno

text = os.environ.get("TEXT", "")
if not text and not sys.stdin.isatty():
    text = sys.stdin.read().rstrip("\n")
if not text:
    print("Error: nothing to encode: give text=, or pipe it on stdin (a secret must use stdin)", file=sys.stderr)
    sys.exit(2)

fmt = os.environ["FORMAT"].strip().lower()
scale = int(os.environ["SCALE"])
border = int(os.environ["BORDER"])
qr = segno.make(text, error=os.environ["ERROR"].strip().upper(), micro=False)

out = f"/output/qr.{fmt}"
qr.save(out, scale=scale, border=border)
width, height = qr.symbol_size(scale=scale, border=border)
print(f"QR version {qr.version}, error {qr.error.upper()}, {width}x{height} px -> qr.{fmt}")

if os.environ["SHOW"].strip().lower() in ("1", "true", "yes"):
    qr.terminal(border=border, compact=True)