Skip to main content
https://img.shields.io/github/actions/workflow/status/adamchainz/treepoem/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/pypi/v/treepoem.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

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:

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:

  1. Run ./download_bwipp.py with the version of BWIPP to download.

  2. Run ./make_data.py to update the barcode types that treepoem knows about.

  3. Add a note in CHANGELOG.rst about the upgrade, adapting from the previous one.

  4. 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)

Source distribution for treepoem 3.28.0
File Size Uploaded
treepoem-3.28.0.tar.gz 377.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for treepoem 3.28.0
File Interpreter ABI Platform
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 log

Release 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

Release history Release notifications | RSS feed

This release

3.28.0 This release

2 release files

3.27.1

2 release files

3.27.0

2 release files

3.25.0

2 release files

3.24.0

2 release files

3.22.0

2 release files

3.21.0

2 release files

3.20.0

2 release files

3.19.0

2 release files

3.18.0

2 release files

3.17.0

2 release files

3.16.0

2 release files

3.15.0

2 release files

3.14.0

2 release files

3.13.0

2 release files

3.12.0

2 release files

3.11.0

2 release files

3.10.0

2 release files

3.9.0

2 release files

3.8.0

2 release files

3.7.0

2 release files

3.6.0

2 release files

3.5.0

2 release files

3.4.0

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.4.1

1 release file

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page