Barcode rendering for Python supporting QRcode, Aztec, PDF417, I25, Code128, Code39 and many more types.
Project description
A cleverly named, but very simple python barcode renderer wrapping the BWIPP library and ghostscript command line tool.
Installation
Install from pip:
python -m pip install treepoem
Python 3.7 to 3.11 supported.
You’ll also need Ghostscript installed. On Ubuntu/Debian this can be installed with:
apt-get install ghostscript
On Mac OS X use:
brew install ghostscript
Otherwise refer to your distribution’s package manager, though it’s likely to be called ghostscript too.
There’s a known issue with rendering on Ghostscript 9.22+ where images are smeared. See GitHub Issue #124 and 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 --version
Working on a Django project? Improve your skills with one of my books.
API
generate_barcode(barcode_type: str, data: str | bytes, options: dict[str, str | bool] | None=None) -> EpsImageFile
Generates a barcode and returns it as a PIL image file object (specifically, a PIL.EpsImagePlugin.EpsImageFile).
barcode_type is the name of the barcode type to generate (see below).
data is a str or bytes of data to embed in the barcode - the amount that can be embedded varies by type.
options is a dictionary of strings-to-strings of extra options to be passed to BWIPP, as per its docs.
For example, this generates a QR code image, and saves it to a file using standard PIL Image.save():
>>> import treepoem
>>> image = 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 coloring, converting the Image object to monochrome as shown above (image.convert('1')) will likely reduce its file size.
barcode_types: dict[str, BarcodeType]
This is a dict of the ~100 names of the barcode types that the vendored version of BWIPP supports: its keys are strs of the barcode type encoder names, and the values are instances of BarcodeType.
BarcodeType
A class representing meta information on the types. It has two attributes:
type_code - the value needed for the barcode_type argument of generate_barcode() to use this type.
description - the human level description of the type which has two str.
Only these common types are used in the test suite:
qrcode - QR Code
azteccode - Aztec Code
pdf417 - PDF417
interleaved2of5 - Interleaved 2 of 5
code128 - Code 128
code39 - Code 39
Command-line interface
Treepoem also includes a simple command-line interface to the functionality of generate_barcode. For example, these commands will generate two QR codes with identical contents, but different levels of error correction (see QR Code Options):
$ treepoem -o barcode1.png -t qrcode "This is a test" eclevel=H
$ treepoem -o barcode2.png -t qrcode "^084his is a test" eclevel=L parse
Complete usage instructions are shown with treepoem --help.
What’s so clever about the name?
Barcode.
Bark ode.
Tree poem.
Updating BWIPP
For development of treepoem, when there’s a new BWIPP release:
Download the latest monolithic zip file from https://github.com/bwipp/postscriptbarcode/releases
Unzip the files into src/treepoem/postscriptbarcode
Remove the unneded docs subdirectory.
Run make_data.py from the root of the repo to update the barcode types that treepoem knows about.
Add a CHANGELOG note about the upgrade.
Commit and make a pull request, for examples see https://github.com/adamchainz/treepoem/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aclosed+upgrade+bwipp
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for treepoem-3.17.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ed12a50d8641778cd0cee088e1d36b6f640db6e7df0c9f4024f4509cb44a4b3 |
|
MD5 | 947deb97bec9579a8d23a4fb1e042367 |
|
BLAKE2b-256 | 584d5e2a736c76590b7e4b4d98aa6c05a136ad6d0fea80254932a8702b5683b3 |