Read and write Data Matrix barcodes from Python 2 and 3.
Project description
Read and write Data Matrix barcodes from Python 2 and 3 using the libdmtx library.
- Pure python
- Works with PIL / Pillow images, OpenCV / numpy ndarrays, and raw bytes
- Decodes locations of barcodes
- No dependencies, other than the libdmtx library itself
- Tested on Python 2.7, and Python 3.4 to 3.7
The older pydmtx package is stuck in Python 2.x-land.
Installation
The libdmtx DLLs are included with the Windows Python wheels. On other operating systems, you will need to install the libdmtx shared library.
Mac OS X:
brew install libdmtx
Linux:
sudo apt-get install libdmtx0a
Install this Python wrapper; use the second form to install dependencies of the read_datamatrix and write_datamatrix command-line scripts:
pip install pylibdmtx pip install pylibdmtx[scripts]
Example usage
The decode function accepts instances of PIL.Image.
>>> from pylibdmtx.pylibdmtx import decode >>> from PIL import Image >>> decode(Image.open('pylibdmtx/tests/datamatrix.png')) [Decoded(data='Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)), Decoded(data='Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]
It also accepts instances of numpy.ndarray, which might come from loading images using OpenCV.
>>> import cv2 >>> decode(cv2.imread('pylibdmtx/tests/datamatrix.png')) [Decoded(data='Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)), Decoded(data='Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]
You can also provide a tuple (pixels, width, height)
>>> image = cv2.imread('pylibdmtx/tests/datamatrix.png') >>> height, width = image.shape[:2] >>> decode((image.tobytes(), width, height)) [Decoded(data='Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)), Decoded(data='Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]
The encode function generates an image containing a Data Matrix barcode:
>>> from pylibdmtx.pylibdmtx import encode >>> encoded = encode('hello world') >>> img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels) >>> img.save('dmtx.png')
Windows error message
If you see an ugly ImportError when importing pylibdmtx on Windows you will most likely need the Visual C++ Redistributable Packages for Visual Studio 2013. Install vcredist_x64.exe if using 64-bit Python, vcredist_x86.exe if using 32-bit Python.
Limitations
Feel free to submit a PR to address any of these.
- I took the bone-headed approach of copying the logic in pydmtx’s decode function (in pydmtxmodule.c); there might be more of libdmtx’s functionality that could usefully be exposed
- I exposed the bare minimum of functions, defines, enums and typedefs neede to reimplement pydmtx’s decode function
Contributors
- Vinicius Kursancew (@kursancew) - first implementation of barcode writing
- Joseph Weston (@jbweston) - support for libdmtx 0.7.5
License
pylibdmtx is distributed under the MIT license (see LICENCE.txt). The libdmtx shared library is distributed under the Simplified BSD license (see libdmtx-LICENCE.txt).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size pylibdmtx-0.1.9-py2.py3-none-any.whl (22.8 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes View |
Filename, size pylibdmtx-0.1.9-py2.py3-none-win32.whl (57.8 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes View |
Filename, size pylibdmtx-0.1.9-py2.py3-none-win_amd64.whl (65.4 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes View |
Hashes for pylibdmtx-0.1.9-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 709f80fafa1f79bf02c9393a8931fd1e7c1e04f8bf3ca9112fa2229751682bed |
|
MD5 | 915bfa35304520b0685036d36991011d |
|
BLAKE2-256 | a86a2cc9d8efd7902dc024927141cb5b0ff19faff201c685863dc2b125a33a56 |
Hashes for pylibdmtx-0.1.9-py2.py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c75c4c9463e3872b3ab10e0b2fd8d906a9cede6f865e924a4f2d9ec766f0a2d |
|
MD5 | adbbc8dfae16a53fd0c3afc865663fa7 |
|
BLAKE2-256 | 2890bc00112a763ff89cd2b2e10c857ad0c7e528e07238aa5b078ff187ba11f3 |
Hashes for pylibdmtx-0.1.9-py2.py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 267d6e75b5a62c177697f35e154249d24baa33cb8a331079fbeeffd7858cb848 |
|
MD5 | 7f5616ffc46c2b61a38d88cce6728d27 |
|
BLAKE2-256 | 918a4694f3214da07dc488c422c355415a860024126b2714d8f4bf1f73419587 |