Code 128 ******** Code 128 is a high-density 1D symbology covering the full ASCII range. pyStrich automatically switches between code sets A, B and C to minimise symbol length, and computes the mod-103 checksum for you. See also: Code 128 on Wikipedia for background on the symbology itself. Example ======= from pystrich.code128 import Code128Encoder encoder = Code128Encoder("WDBCA45D2HA327260") encoder.save_svg("code128-example.svg") [image: Code 128 barcode encoding "WDBCA45D2HA327260".][image] Sizing, label, font and layout ============================== The "bar_width" argument to "save()" and "get_imagedata()" sets the pixel width of the narrowest bar (default "3"). The "options" dict passed to "Code128Encoder" controls the human- readable label and the surrounding layout. All keys are optional. "show_label" Whether to render the human-readable label underneath the bars. Defaults to "True"; set to "False" to suppress it. "ttf_font" Absolute path to a TrueType font file used for the label. Defaults to a bundled bitmap font if unset. "ttf_fontsize" Font size in points. "height" Total image height in pixels. Defaults to roughly a third of the image width. "label_border" Pixels of vertical space between the bars and the label. "bottom_border" Pixels of vertical space between the label and the bottom edge. See also: Printing barcodes for guidance on selecting "bar_width" for printed output. options = { "height": 200, "label_border": 10, "bottom_border": 10, "ttf_fontsize": 24, # "ttf_font": "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", } encoder = Code128Encoder("WDBCA45D2HA327260", options=options) encoder.save("code128-custom.png", bar_width=4) [image: Code 128 barcode encoding "WDBCA45D2HA327260" with a taller image and larger label.][image] 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). Code128Encoder("WDBCA45D2HA327260").save_svg("code128.svg") [image: SVG Code 128 barcode encoding "WDBCA45D2HA327260".][image] The SVG's "viewBox" is in module units (one narrow bar = one unit), while "width" and "height" scale by "bar_width". The 10-module quiet zones mandated by the standard are applied automatically on each side. 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. Code128Encoder("WDBCA45D2HA327260").save("code128.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). Code128Encoder("WDBCA45D2HA327260").save_eps("code128.eps") The "bar_width" argument is the width of the narrowest bar in PostScript points (1 point = 1/72 inch). The 10-module quiet zones are applied automatically. Added in version 0.12. API === class Code128Encoder(text: str, options: BarcodeRenderOptions | None = None) Bases: "Bar1DEncoder" Encode a string as a Code 128 1D barcode. Code sets A, B and C are switched between automatically to minimise symbol length. The mod-103 checksum is computed and appended for you. Typical use: encoder = Code128Encoder("nm0000385") encoder.save("barcode.png") Variables: * **text** -- The original input text. * **encoded_text** -- List of code values produced by the text encoder, including start codes and code-set switches. * **checksum** -- The mod-103 checksum value. * **bars** -- The bar/space pattern as a string of ""1"" and ""0"". * **options** -- Render-time options dict (empty if none were supplied). * **width** -- Pixel width of the most recently rendered image. "0" until a render method has been called. * **height** -- Pixel height of the most recently rendered image. get_eps(bar_width: int = 3) -> str Render the barcode and return EPS markup. Parameters: **bar_width** -- Width in PostScript points of the narrowest bar. Return type: str Added in version 0.12. get_imagedata(bar_width: int = 3) -> bytes Render the barcode and return PNG bytes. Parameters: **bar_width** -- Width in pixels of the narrowest bar. Returns: PNG-encoded image data. Return type: bytes get_pilimage(bar_width: int = 3) -> PIL.Image.Image Render the barcode and return a Pillow image. Parameters: **bar_width** -- Width in pixels of the narrowest bar. Returns: The rendered barcode. Return type: PIL.Image.Image Added in version 0.11. get_svg(bar_width: int = 3) -> str Render the barcode and return SVG markup. Parameters: **bar_width** -- Width in user units of the narrowest bar. Return type: str Added in version 0.12. save(filename: str | PathLike[str], bar_width: int = 3) -> None Render the barcode to a PNG file. Parameters: * **filename** -- Path to write the PNG to. * **bar_width** -- Width in pixels of the narrowest bar. save_eps(filename: str | PathLike[str], bar_width: int = 3) -> None Save the barcode as an EPS file. Pass an ".eps" filename. Parameters: * **filename** -- EPS output path. * **bar_width** -- Width in PostScript points of the narrowest bar. Added in version 0.12. save_svg(filename: str | PathLike[str], bar_width: int = 3) -> None Save the barcode as an SVG file. Pass a ".svg" filename. Parameters: * **filename** -- SVG output path. * **bar_width** -- Width in user units of the narrowest bar. Added in version 0.12. calculate_check_sum() -> int Compute the Code 128 mod-103 checksum for "encoded_text". The start code contributes with weight 1; subsequent symbols are weighted by their 1-based position before the modulo is taken. init_renderer() -> Code128Renderer Construct a "Code128Renderer" for the encoded bars. Return type: "Code128Renderer"