A cleverly named, but very simple python barcode renderer wrapping the BWIPP library and ghostscript command line tool.
Improve your Django and Git skills with one of my books.
Installation
Install from pip:
python -m pip install treepoem
Python 3.9 to 3.14 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
API
generate_barcode(barcode_type: str, data: str | bytes, options: dict[str, str | bool] | None=None, *, scale: int = 2) -> Image
Generates a barcode and returns it as a PIL Image object
barcode_type is the name of the barcode type to generate from BWIPP’s list of supported types. The barcode_types dictionary, described below, contains the names of the supported types.
data is a str or bytes of data to embed in the barcode. The amount of data that can be embedded varies by type.
options is a dictionary of strings-to-strings of BWIPP options, per its documentation.
scale controls the output image size. Use 1 for the smallest image and larger values for larger images.
For example, this generates a QR code image, adds a border with ImageOps.expand(), and saves it to a file using Image.save():
import treepoem
from PIL import ImageOps
image = treepoem.generate_barcode(
barcode_type="qrcode",
data="https://example.com",
)
image = ImageOps.expand(image, border=10, fill="white")
image.convert("1").save("barcode.png")
For greyscale barcodes, the conversion to monochrome (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:
Run ./download_bwipp.py with the version of BWIPP to download.
Run ./make_data.py to update the barcode types that treepoem knows about.
Add a note in CHANGELOG.rst about the upgrade, adapting from the previous one.
Commit and make a pull request, adapting from previous examples.
Release files for treepoem 3.28.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| treepoem-3.28.0.tar.gz | 377.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| treepoem-3.28.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 751.2 kB
Release files / treepoem-3.28.0.tar.gz
| Download URL | treepoem-3.28.0.tar.gz |
|---|---|
| Size | 377.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
665ea9d27e2e9a7421b9650f6fa611b84245854d2ff8d4295bd2ff3f1b02e55e
|
|
BLAKE2b-256 checksum How to use checksums |
e9d7d3fd098dcbc5b74b4152c90a3ccad06f14e03c554b37881e687640b34438
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2025.
Transparency logRelease files / treepoem-3.28.0-py3-none-any.whl
| Download URL | treepoem-3.28.0-py3-none-any.whl |
|---|---|
| Size | 373.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70962b62d44b2bd97c6f9be0558da6037f1cca4264c4755ffc8b984940753f34
|
|
BLAKE2b-256 checksum How to use checksums |
e070ffe884002c911b66955e70d732cb132a9e1b722246c322318077d83b540e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2025.
Transparency log