QR Code¶
QR Code is a 2D symbology widely used for URLs and contact details.
See also
QR code on Wikipedia for background on the symbology itself.
QR codes are defined in ISO/IEC 18004.
Note
The QR support in pyStrich is less actively maintained than the other symbologies. If your project’s main need is QR codes, consider whether python-qrcode is a better fit – it has a larger feature set (logos, styled modules). pyStrich’s QR support is most useful when you also need one of its other symbologies and want a single dependency.
Example¶
from pystrich.qrcode import QRCodeEncoder
encoder = QRCodeEncoder("https://github.com/mmulqueen/pyStrich")
encoder.save_svg("qrcode-example.svg")
Sizing and quiet zone¶
The cellsize argument to save() and
get_imagedata() sets the pixel side length of one
module (default 5).
See also
Printing barcodes for guidance on selecting cellsize for printed
output.
encoder = QRCodeEncoder("https://github.com/mmulqueen/pyStrich")
encoder.save("qrcode-large.png", cellsize=10)
Output formats¶
SVG output¶
For embedding in web pages or any workflow that benefits from
resolution-independent output, use save_svg() (or
get_svg() to receive the SVG as a string).
from pystrich.marks import MarkShape
QRCodeEncoder("https://github.com/mmulqueen/pyStrich").save_svg("qr.svg")
QRCodeEncoder("https://github.com/mmulqueen/pyStrich").save_svg(
"qr-circles.svg", mark_shape=MarkShape.CIRCULAR_CELLS
)
Default |
|
|---|---|
The SVG’s viewBox is in module units, while width and height
scale by cellsize. The mark_shape keyword selects how matched
cells are drawn – horizontal runs of rectangles (the default) or one
filled circle per cell.
Note
Circular cells fall outside the standard module shape and decoder support varies. Test with your target scanner before deploying.
Added in version 0.12.
PNG output¶
For raster output, use save() to write a PNG file or
get_imagedata() to receive the raw PNG bytes.
QRCodeEncoder("https://github.com/mmulqueen/pyStrich").save("qrcode.png")
EPS output¶
For embedding in LaTeX (\includegraphics) or other vector print
workflows, use save_eps() (or
get_eps() to receive the EPS as a string).
QRCodeEncoder("https://github.com/mmulqueen/pyStrich").save_eps("qr.eps")
The cellsize argument is the side length of one module in PostScript
points (1 point = 1/72 inch).
Added in version 0.12.
Terminal output¶
For quick on-screen display, get_terminal_art()
returns a scannable rendering using Unicode half-block characters. Each
character represents two matrix rows and one column, so cells appear
roughly square in a typical fixed-width terminal font.
print(QRCodeEncoder("https://github.com/mmulqueen/pyStrich").get_terminal_art())
█▀▀▀▀▀█ █▄█▀▀▀▄ ███▀▀ █▀▀▀▀▀█
█ ███ █ ▄▀██ ▀ █▀▄ ▀█ █ ███ █
█ ▀▀▀ █ ▄█▄█▀█▀▀▀▀█▄ █ ▀▀▀ █
▀▀▀▀▀▀▀ █ ▀ ▀▄█ ▀▄█ ▀ ▀▀▀▀▀▀▀
█ ▀▀ ▀▀█ ▄█▄▀▀▀▀█▀▀▄▄ █▄▄▀ ▀█
▄█ ▄▀▀▀█ ▄ ▀▄█▄▄▀█▄▄█▄▀▄ ▀▀▄
█▄ ▄█▄▀▀▀█▀█▀▄ ███ █ ▄ ▀█▄▄
▀▄▄█ ▀▀ █▀▄▄▀▄█ ▀██▄█▄▄ ▀█▀
▄▀▀▄██▀▄▀▀█ ▀▄ ▄▀ ▀▀▀ ▄▀█▄█
▄█▄██ ▀▀█▄ █ █▀▄▄ ▀ ███ █
▀ ▀ ▀▀▀▄▀██ █▄▄▄██ █▀▀▀███▄▄
█▀▀▀▀▀█ █▄▄▀ ▀ █ ▀▀█ ▀ ██ █▄
█ ███ █ ▄▄█▄▄ █▄█▀▀▄▀█▀██▄▀ ▄
█ ▀▀▀ █ ▀▀ ▀██ ▄▀▀▀ █▄ █ ▄▀▄▀
▀▀▀▀▀▀▀ ▀▀ ▀ ▀▀ ▀ ▀▀▀▀▀ ▀
By default the output is wrapped in ANSI escape codes that force a white
background and black foreground, so the symbol scans regardless of the
terminal’s colour scheme. Pass ansi_bg=False for plain output (correct
only on a light-themed terminal).
Added in version 0.12.
DXF (CAD) output¶
For direct part marking applications, get_dxf()
returns a DXF representation of the symbol that CAD and CAM tools can read
directly. The cellsize is in your chosen units (default "mm")
rather than pixels.
encoder = QRCodeEncoder("WDBCA45D2HA327260")
with open("part.dxf", "w") as f:
f.write(encoder.get_dxf(cellsize=0.5, units="mm"))
The default inverse=True emits geometry for the light modules,
including the quiet zone – so the bounding box frames the symbol. Pass
inverse=False to emit only the dark modules instead, matching the
symbol’s normal appearance; the bounding box then hugs the dark cells and
the quiet zone has to be reintroduced downstream.
Error correction level¶
QR Codes embed redundant data so that a partly-damaged symbol can still be read. The error correction level (ECL) sets how much redundancy is added, and is one of:
|
Low: ~7% of codewords recoverable. Smallest symbol. |
|
Medium: ~15%. Default if |
|
Quartile: ~25%. |
|
High: ~30%. Largest symbol; tolerates the most damage. |
Higher levels produce a denser symbol for the same payload (or, equivalently,
require a larger symbol to fit the same payload), so pick the lowest level
that meets your durability needs. "H" is typically reserved for symbols
that may be partly obscured (e.g. by a logo) or printed on surfaces likely
to be scratched, smudged or torn.
QRCodeEncoder("https://en.wikipedia.org/wiki/Kings_River_(California)", ecl="H").save("qr-high.png")
GS1 Digital Link¶
GS1 Digital Link
encodes a GTIN (with optional batch, expiry, serial, …) as a URL.
It’s just a URL – pass it to QRCodeEncoder directly:
QRCodeEncoder("https://id.gs1.org/01/05050070007664/10/W126").save("dl.png")
Raw AI-syntax GS1 QR (FNC1 mode indicator) is not supported; for that, use Data Matrix.
Non-ASCII text¶
QRCodeEncoder accepts any Unicode string directly and picks the
narrowest character set that fits. Wrap the input in QRCodeData
only when you want to constrain that choice – for example, to enforce
"ascii" so a stray non-ASCII character raises instead of silently
growing the symbol:
Encoding |
Behaviour |
|---|---|
|
Raises |
|
Latin-1. Declares ECI 3 at the start of the symbol so decoders do not fall back to Shift-JIS heuristics on high bytes. |
|
Declares ECI 26 and byte-encodes the input. Conformant decoders pick up the encoding automatically. |
Tip
The auto-selected encoding is always the narrowest one that fits, so
passing a plain str already gives you the smallest symbol. Picking
an encoding by hand is mostly useful for input validation – e.g.
reject anything outside ASCII at the boundary.
# Plain str: Latin-1 picked automatically, ECI 3 emitted.
QRCodeEncoder("Ich dachte, Sie wären kräftiger").save("latin1.png")
# Plain str: UTF-8 picked automatically, ECI 26 emitted.
QRCodeEncoder("€5 親切にしろ 🐻❄️").save("utf8.png")
If you pin an encoding that does not fit the input, the raised error names the offending character and suggests the encoding that would have worked:
>>> from pystrich.qrcode import QRCodeData
>>> QRCodeData("Ich dachte, Sie wären kräftiger", encoding="ascii")
Traceback (most recent call last):
...
pystrich.exceptions.PyStrichInvalidInput: QRCodeData encoding 'ascii' expects ASCII; got 'ä'. Try QRCodeData('Ich dachte, Sie wären kräftiger', encoding='iso-8859-1') or pass auto_encoding=True to select an encoding automatically.
Anatomy¶
A QR Code carries the payload in a sea of data modules, surrounded by several fixed function patterns that let scanners locate, align and decode the symbol. The diagram below labels a Version 5 symbol (37x37 modules); larger versions repeat the same parts and add more alignment patterns, plus a version-information block from Version 7 upwards.
Position detection patterns – the three large squares at the top-left, top-right and bottom-left corners. Scanners use them to detect the symbol from any angle and infer its orientation from the one missing corner.
Separators – a one-module strip of white isolating each finder from the data area, keeping the finder pattern unambiguous.
Timing patterns – one row and one column of alternating dark and light cells, running between the top finders and between the left finders. They fix the module grid across the symbol.
Alignment pattern – smaller squares dropped into the data area in Version 2 and above to correct for projective distortion when the symbol is photographed at an angle. Version 5 has one; the largest versions have several dozen.
Data and error correction – the masked codeword stream (payload plus Reed-Solomon). This is what scales with version.
Quiet zone – four modules of white margin on every side, as mandated by the QR Code specification.
API¶
- class QRCodeEncoder(text: str | QRCodeData, ecl: Literal['L', 'M', 'Q', 'H'] | None = None)[source]¶
Bases:
Matrix2DEncoder[int]Encode text as a QR Code 2D barcode.
A plain
stris encoded with the narrowest character set that fits: ASCII (no ECI), Latin-1 (ECI 3), or UTF-8 (ECI 26). Pass aQRCodeDatato pin the encoding explicitly.Typical use:
encoder = QRCodeEncoder("https://d-nb.info/gnd/135514053") encoder.save("qr.png")
- Variables:
matrix – 2D list describing the symbol prior to rendering.
width – Pixel width of the most recently rendered image.
height – Pixel height of the most recently rendered image.
- init_renderer() QRCodeRenderer[source]¶
Construct a
QRCodeRendererfor the encoded matrix.Updates
widthandheightwith the renderer’s dimensions and returns the renderer.
- get_dxf(cellsize: float = 1.0, inverse: bool = True, units: Literal['in', 'ft', 'mi', 'mm', 'cm', 'm'] | None = 'mm', *, mark_shape: MarkShape = MarkShape.SQUARE_CELLS) str¶
Return a DXF (CAD) representation of the symbol.
- Parameters:
cellsize – Side length of one module in
units.inverse – If
True(the default), light modules are drawn as filled cells. IfFalse, dark modules are drawn, matching the normal appearance of the symbol.units – One of
"in","ft","mi","mm","cm"or"m", orNonefor Unspecified ($INSUNITS=0).mark_shape – How matched cells are grouped and drawn.
- Return type:
Added in version 0.9.
Changed in version 0.12:
unitsnow supports"in","ft","mi","cm","m"andNone(Unspecified); previously any value other than"mm"was silently treated as unspecified.
- get_eps(cellsize: int = 5, *, inverse: bool = False, mark_shape: MarkShape = MarkShape.HORIZONTAL_RUNS) str¶
Render the symbol and return EPS markup.
- Parameters:
cellsize – Side length in PostScript points of one module.
inverse – If
True, mark the light cells instead of the dark ones.mark_shape – How matched cells are grouped and drawn.
- Return type:
Added in version 0.12.
- get_imagedata(cellsize: int = 5) bytes¶
Render the symbol and return PNG bytes.
- Parameters:
cellsize – Side length in pixels of one module.
- Returns:
PNG-encoded image data.
- Return type:
- get_pilimage(cellsize: int = 5) PIL.Image.Image¶
Render the symbol and return a Pillow image.
- Parameters:
cellsize – Side length in pixels of one module.
- Returns:
The rendered symbol.
- Return type:
Added in version 0.11.
- get_svg(cellsize: int = 5, *, inverse: bool = False, mark_shape: MarkShape = MarkShape.HORIZONTAL_RUNS) str¶
Render the symbol and return SVG markup.
- Parameters:
cellsize – Side length in user units of one module.
inverse – If
True, mark the light cells instead of the dark ones.mark_shape – How matched cells are grouped and drawn.
- Return type:
Added in version 0.12.
- get_terminal_art(*, ansi_bg: bool = True) str¶
Render the symbol using Unicode half-block characters for terminals.
Each character represents two matrix rows and one column, producing approximately square cells in a typical fixed-width font and yielding a result that is scannable on screen.
- Parameters:
ansi_bg – If
True(the default), wrap each line in ANSI escape codes that force a white background and black foreground, making the symbol scannable regardless of the terminal’s colour scheme. Set toFalsefor plain output (correct only on a light-themed terminal).- Return type:
Added in version 0.12.
- save(filename: str | PathLike[str], cellsize: int = 5) None¶
Save the symbol as a PNG. Pass a
.pngfilename.- Parameters:
filename – PNG output path.
cellsize – Side length in pixels of one module.
- save_eps(filename: str | PathLike[str], cellsize: int = 5, *, inverse: bool = False, mark_shape: MarkShape = MarkShape.HORIZONTAL_RUNS) None¶
Save the symbol as an EPS file. Pass an
.epsfilename.- Parameters:
filename – EPS output path.
cellsize – Side length in PostScript points of one module.
inverse – If
True, mark the light cells instead of the dark ones.mark_shape – How matched cells are grouped and drawn.
Added in version 0.12.
- save_svg(filename: str | PathLike[str], cellsize: int = 5, *, inverse: bool = False, mark_shape: MarkShape = MarkShape.HORIZONTAL_RUNS) None¶
Save the symbol as an SVG file. Pass a
.svgfilename.- Parameters:
filename – SVG output path.
cellsize – Side length in user units of one module.
inverse – If
True, mark the light cells instead of the dark ones.mark_shape – How matched cells are grouped and drawn.
Added in version 0.12.
- class QRCodeData(*segments: str, encoding: Literal['ascii', 'iso-8859-1', 'utf-8'] | None = None, auto_encoding: bool = False)¶
Bases:
objectEncoder input with an explicit character-set choice.
QRCodeEncoderaccepts a plainstrand selects the encoding automatically. UseQRCodeDataonly to pin the encoding – for example, force"ascii"to reject non-ASCII input.Pass either
encoding=(one of"ascii","iso-8859-1","utf-8") orauto_encoding=True. Withauto_encoding=Truethe constructor picks the narrowest fitting encoding; anyencoding=argument is then ignored.- Variables:
segments – Tuple of input string segments.
encoding – The chosen Python codec name.
auto_encoding –
Trueif the encoding was picked automatically.