treepoem
treepoem is a Python library (current version 3.28.0) for rendering various barcode types including QR code, Aztec, PDF417, I25, Code128, and Code39. It acts as a wrapper around the open-source BWIPP PostScript barcode library and utilizes the Ghostscript command-line tool for rendering. The library is actively maintained and typically sees frequent updates to its vendored BWIPP component.
Warnings
- gotcha Older Ghostscript versions (e.g., 9.22+) may produce smeared barcode images. While a fix was merged in Ghostscript 9.26, some smearing might still occur with certain barcodes.
- gotcha treepoem relies on an external Ghostscript installation, which is a non-Python dependency. This can complicate deployment in environments where installing system-level packages is restricted (e.g., some serverless functions, shared hosting).
- gotcha Generating multiple barcodes in a tight loop can be slow because treepoem re-initializes and reopens the Ghostscript subprocess for each call to `generate_barcode()`.
Install
-
pip install treepoem -
sudo apt-get install ghostscript -
brew install ghostscript
Imports
- generate_barcode
from treepoem import generate_barcode
- ImageOps
from PIL import ImageOps
Quickstart
import treepoem
from PIL import ImageOps
# Generate a QR code barcode
image = treepoem.generate_barcode(
barcode_type="qrcode",
data="https://www.example.com"
)
# Add a white border and save as a monochrome PNG
image = ImageOps.expand(image, border=10, fill="white")
image.convert("1").save("barcode.png")
print("Generated barcode.png")