Install command-line scripts for pylibdmtx
masterTo use the read_datamatrix and write_datamatrix command-line scripts, you must install Pillow version 3.2.0 or higher.
pip install "Pillow>=3.2.0"repository·master·Indexed 41 days ago
https://github.com/naturalhistorymuseum/pylibdmtxA Python library for reading and writing Data Matrix barcodes, acting as a wrapper around the libdmtx library. It supports Python 2 and 3 and integrates with image processing libraries such as Pillow and OpenCV. Features include decode() for reading barcodes from PIL images or numpy arrays and encode() for generating barcodes from bytes.
To use the read_datamatrix and write_datamatrix command-line scripts, you must install Pillow version 3.2.0 or higher.
pip install "Pillow>=3.2.0"To install the latest version of pylibdmtx directly from GitHub, use the following command. Note that the PyPI package is currently outdated.
On non-Windows systems, you must also install the libdmtx shared library:
brew install libdmtxsudo apt-get install libdmtx0aWindows users have the libdmtx DLLs included in the Python wheels.
pip install git+https://github.com/NaturalHistoryMuseum/pylibdmtx.gitTo develop on pylibdmtx, create a virtual environment, upgrade pip, and install the required dependencies from requirements.txt.
To run tests with coverage reporting, use pytest with the --cov flag. You can also test the command-line script read_datamatrix using a sample image from the tests directory.
# Setup environment
python -m venv venv
source ./venv/bin/activate
pip install -U pip
pip install -r requirements.txt
# Run tests with coverage
python -m pytest --verbose --cov=pylibdmtx --cov-report=term-missing --cov-report=html pylibdmtx
# Run the CLI script example
python -m pylibdmtx.scripts.read_datamatrix pylibdmtx/tests/datamatrix.pngThe release process involves building source and wheel distributions for different platforms, ensuring the correct libdmtx.dll is included in each, and uploading via twine.
To build for Windows, you must swap the MANIFEST.in content between win32 and win64 definitions to prevent cross-pollination of DLLs.
Before releasing to the main PyPI, upload to testpypi and verify the installation in a clean environment. If using Python 2.x, you must manually install enum34 and pathlib.
Once verified, upload the built distributions to PyPI using twine.
# 1. Build process (example for win32)
rm -rf build dist MANIFEST.in pylibdmtx.egg-info
cp MANIFEST.in.all MANIFEST.in
./setup.py bdist_wheel
cat MANIFEST.in.all MANIFEST.in.win32 > MANIFEST.in
./setup.py bdist_wheel --plat-name=win32
# 2. Upload to TestPyPI
twine upload -r testpypi dist/*
# 3. Final Release to PyPI
twine upload dist/*When developing on Windows, the load_pylibdmtx function in wrapper.py expects specific DLL filenames in the pylibdmtx directory to handle different architectures.
To ensure the correct DLL is loaded, save your files as follows:
libdmtx.dll $\rightarrow$ libdmtx-32.dlllibdmtx.dll $\rightarrow$ libdmtx-64.dllTo use the included command-line scripts (like read_datamatrix), install the package with the [scripts] extra.
pip install pylibdmtx[scripts]If you encounter an ImportError when importing pylibdmtx on Windows, you likely need the Visual C++ Redistributable Packages for Visual Studio 2013.
vcredist_x64.exevcredist_x86.exeThe encode function generates an image containing a Data Matrix barcode from provided bytes. The returned object contains pixels, width, and height attributes.
from pylibdmtx.pylibdmtx import encode
from PIL import Image
# Encode string to bytes and generate barcode
encoded = encode('hello world'.encode('utf8'))
# Convert the encoded result to a PIL Image for saving
img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
img.save('dmtx.png')The decode function is used to read Data Matrix barcodes from various image formats. It supports:
PIL.Image instancesnumpy.ndarray (e.g., from OpenCV)(pixels, width, height)Returns a list of Decoded objects containing the data and the rect (location) of the barcode.
from pylibdmtx.pylibdmtx import decode
from PIL import Image
import cv2
# Using PIL
decoded_pil = decode(Image.open('image.png'))
# Using OpenCV (numpy array)
decoded_cv2 = decode(cv2.imread('image.png'))
# Using raw bytes tuple (pixels, width, height)
# image.shape[:2] returns (height, width)
height, width = image.shape[:2]
decoded_raw = decode((image.tobytes(), width, height))