Skip to main content

Barcode rendering for Python supporting QRcode, Aztec, PDF417, I25, Code128, Code39 and many more types.

Project description

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.13 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treepoem-3.27.1.tar.gz (375.5 kB view details)

Uploaded Source

Built Distribution

treepoem-3.27.1-py3-none-any.whl (372.0 kB view details)

Uploaded Python 3

File details

Details for the file treepoem-3.27.1.tar.gz.

File metadata

  • Download URL: treepoem-3.27.1.tar.gz
  • Upload date:
  • Size: 375.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for treepoem-3.27.1.tar.gz
Algorithm Hash digest
SHA256 471769fdbc0e9fce37d9f139446db1763ba1c93d4f823e398b364c2a71f8429c
MD5 04514c6065ef1c3015e46ab7ee876c31
BLAKE2b-256 d275adcea502d2a4832847b096df58fa69a5bfa3581ecaf9dda3fb066a38d304

See more details on using hashes here.

Provenance

The following attestation bundles were made for treepoem-3.27.1.tar.gz:

Publisher: main.yml on adamchainz/treepoem

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file treepoem-3.27.1-py3-none-any.whl.

File metadata

  • Download URL: treepoem-3.27.1-py3-none-any.whl
  • Upload date:
  • Size: 372.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for treepoem-3.27.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8f929b0686fa40bdfe7b2fb0a134a3266481cd321a234ec7a279d7bac07d55a7
MD5 f62874b4eb640c3ebec8fbabd0b3ce0a
BLAKE2b-256 5893b943eed507664fbfe81f27dce690bcebe7d4c67edfd615109e137e8d751c

See more details on using hashes here.

Provenance

The following attestation bundles were made for treepoem-3.27.1-py3-none-any.whl:

Publisher: main.yml on adamchainz/treepoem

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page