Package: treepoem
A cleverly named, but very simple python barcode renderer wrapping theBWIPPlibrary andghostscriptcommand line tool.InstallationInstall frompip:python-mpipinstalltreepoemPython 3.8 to 3.12 supported.You’ll also need Ghostscript installed. On Ubuntu/Debian this can be installed
with:apt-getinstallghostscriptOn Mac OS X use:brewinstallghostscriptOtherwise refer to your distribution’s package manager, though it’s likely to
be calledghostscripttoo.There’s a known issue with rendering on Ghostscript 9.22+ where images are
smeared. SeeGitHub Issue #124and
its associated links for more details. Ghostscript merged a fix in version
9.26 and common barcodes seem to work from then on, though still with some
smearing.You can check your Ghostscript version with:gs--versionWorking on a Django project?Improve your skills withone of my books.APIgenerate_barcode(barcode_type: str, data: str | bytes, options: dict[str, str | bool] | None=None, *, scale: int = 2)->ImageGenerates a barcode and returns it as aPIL Image objectbarcode_typeis the name of the barcode type to generate (see below).datais astrorbytesof data to embed in the barcode - the amount
that can be embedded varies by type.optionsis a dictionary of strings-to-strings of extra options to be passed
toBWIPP, as per its docs.scalecontrols the output image size.
Use1for the smallest image and larger values for larger images.For example, this generates a QR code image, and saves it to a file usingImage.save():importtreepoemimage=treepoem.generate_barcode(barcode_type="qrcode",# One of the BWIPP supported codes.data="barcode payload",)image.convert("1").save("barcode.png")If your barcode image is monochrome, with no additional text or colouring, converting theImageobject to monochrome (image.convert("1")) will likely reduce its file size.barcode_types: dict[str, BarcodeType]This is adictof the ~100 names of the barcode types that the vendored
version ofBWIPPsupports: its keys arestrs of the barcode type encoder
names, and the values are instances ofBarcodeType.BarcodeTypeA class representing meta information on the types. It has two attributes:type_code- the value needed for thebarcode_typeargument ofgenerate_barcode()to use this type.description- the human level description of the type
which has twostr.Only these common types are used in the test suite:qrcode-QR Codeazteccode-Aztec Codepdf417-PDF417interleaved2of5-Interleaved 2 of 5code128-Code 128code39-Code 39Command-line interfaceTreepoem also includes a simple command-line interface to the
functionality ofgenerate_barcode. For example, these commands
will generate two QR codes with identical contents, but different levels
of error correction (seeQR Code Options):$treepoem-obarcode1.png-tqrcode"This is a test"eclevel=H$treepoem-obarcode2.png-tqrcode"^084his is a test"eclevel=LparseComplete usage instructions are shown withtreepoem--help.What’s so clever about the name?Barcode.Bark ode.Tree poem.Updating BWIPPFor development of treepoem, when there’s a new BWIPP release:Run./download_bwipp.pywith the version of BWIPP to download.Run./make_data.pyto update the barcode types that treepoem knows about.Add a note inCHANGELOG.rstabout the upgrade, adapting from the previous one.Commit and make a pull request,adapting from previous examples.