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/workflow/status/adamchainz/treepoem/CI/master?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.

Installation

Install from pip:

python -m pip install treepoem

Python 3.6 to 3.9 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? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.


API

generate_barcode(barcode_type, data, options=None)

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 (Python 2 unicode) or bytes (Python 2 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

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. Download the latest monolithic zip file from https://github.com/bwipp/postscriptbarcode/releases

  2. Unzip the files into src/treepoem/postscriptbarcode

  3. Remove the unneded docs subdirectory.

  4. Run make_data.py from the root of the repo to update the barcode types that treepoem knows about.

  5. Add a CHANGELOG note about the upgrade.

  6. Commit and make a pull request, for examples see https://githugb.com/adamchainz/treepoem/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aclosed+upgrade+bwipp

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.7.0.tar.gz (429.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

treepoem-3.7.0-py3-none-any.whl (403.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: treepoem-3.7.0.tar.gz
  • Upload date:
  • Size: 429.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for treepoem-3.7.0.tar.gz
Algorithm Hash digest
SHA256 10df22747d839290fb162fda1d1229e84753b929c20700afbb3a771b02c201b5
MD5 33f216b2abc86b23178008a9d0230ba9
BLAKE2b-256 ed2bd560709a478af5a172d2b3f4d2d47cd456da3c4d1b2d27a66c20a2562b1b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: treepoem-3.7.0-py3-none-any.whl
  • Upload date:
  • Size: 403.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for treepoem-3.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 630b3cc94cdf189d01429577d1954228d9c9494ad04cfed1c36cb99a8b8287dd
MD5 457f6325fac221037e9698b73ebe3092
BLAKE2b-256 9673c1ed6762e64750b63e186e7c871fb181af6259d95dbfd5ed9fac441757d5

See more details on using hashes here.

Supported by

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