pylibdmtx
raw JSON → 0.1.10 verified Mon Apr 27 auth: no python
Python library for reading and writing Data Matrix barcodes, wrapping the libdmtx C library. Current version 0.1.10, low release cadence.
pip install pylibdmtx Common errors
error OSError: libdmtx.so: cannot open shared object file ↓
cause libdmtx shared library not installed on Linux/macOS.
fix
Install libdmtx via system package manager: apt-get install libdmtx0 (Debian/Ubuntu) or brew install libdmtx (macOS).
error ImportError: cannot import name 'decode' from 'pylibdmtx' ↓
cause Importing from the top-level package instead of the submodule.
fix
Use: from pylibdmtx.pylibdmtx import decode
error AttributeError: 'NoneType' object has no attribute 'decode' ↓
cause decode() returned None (no barcode found).
fix
Check result is not None before accessing .data.
error TypeError: expected str, bytes or os.PathLike object, not BytesIO ↓
cause Passing a BytesIO object directly instead of a PIL Image.
fix
Open with Image.open(BytesIO(...)) to create PIL Image.
Warnings
breaking On Windows, libdmtx.dll must be in PATH. Use conda or manually add DLL directory. ↓
fix Install via conda-forge (includes bundled DLL) or copy libdmtx.dll to a PATH directory.
gotcha decode() returns bytes, not str. Must call .decode('utf-8') on data. ↓
fix Use result.data.decode('utf-8') or appropriate encoding.
gotcha Input image must be PIL Image, not file path. Open with Image.open() first. ↓
fix Always pass a PIL Image object to decode().
deprecated Python 2 support is deprecated; library still installs but may break on Python 2 EOL. ↓
fix Use Python 3.6+.
Install
conda install -c conda-forge pylibdmtx Imports
- decode wrong
import pylibdmtxcorrectfrom pylibdmtx.pylibdmtx import decode - encode
from pylibdmtx.pylibdmtx import encode
Quickstart
from PIL import Image
from pylibdmtx.pylibdmtx import decode
image = Image.open('datamatrix.png')
results = decode(image)
for result in results:
print(result.data.decode('utf-8'))