Giter Club home page Giter Club logo

treepoem's Introduction

Treepoem

https://img.shields.io/github/actions/workflow/status/adamchainz/treepoem/main.yml?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.

Installation

Install from pip:

python -m pip install treepoem

Python 3.8 to 3.12 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, *, 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 (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.

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, and saves it to a file using 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 colouring, converting the Image object 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.