Skip to main content
https://img.shields.io/badge/python-2.7%2C%203.5%2C%203.6%2C%203.7%2C%203.8%2C%203.9%2C%203.10-blue.svg https://badge.fury.io/py/pylibdmtx.svg https://img.shields.io/github/workflow/status/NaturalHistoryMuseum/pylibdmtx/Tests/master?label=tests https://coveralls.io/repos/github/NaturalHistoryMuseum/pylibdmtx/badge.svg?branch=master

Read and write Data Matrix barcodes from Python 2 and 3 using the libdmtx library.

  • Pure python

  • Works with PIL / Pillow images, OpenCV / imageio / 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.5 to 3.10

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'.encode('utf8'))
>>> img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
>>> img.save('dmtx.png')
>>> print(decode(Image.open('dmtx.png')))
[Decoded(data=b'hello world', rect=Rect(left=9, top=10, width=80, height=79))]

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).

Metadata

Release files for pylibdmtx 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for pylibdmtx 0.1.10
File Interpreter ABI Platform
pylibdmtx-0.1.10-py2.py3-none-win_amd64.whl Python 2, Python 3 none Windows x86-64 Details
pylibdmtx-0.1.10-py2.py3-none-win32.whl Python 2, Python 3 none Windows x86-32 Details
pylibdmtx-0.1.10-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 147.0 kB

Release files / pylibdmtx-0.1.10-py2.py3-none-win_amd64.whl

Download URL pylibdmtx-0.1.10-py2.py3-none-win_amd64.whl
Size 65.7 kB
Tags Python 2 Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
e8b0140ab5f473c67fa4335333994a99ea9f683888ae892b1af2dc045560331b
BLAKE2b-256 checksum
How to use checksums
c14bd3fb82bd33d3b8bc0143f17c69660f99f830bd7326d0a09c3c1e1cc01771
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / pylibdmtx-0.1.10-py2.py3-none-win32.whl

Download URL pylibdmtx-0.1.10-py2.py3-none-win32.whl
Size 58.1 kB
Tags Python 2 Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
2adb8dd71bd138159f6433dafd8eec7420eef97aaf8e51cf0f4fdd976f109962
BLAKE2b-256 checksum
How to use checksums
b95774d28fba3205a76cd3e4c16d693da54b21a10df6a28fb688f7100c153d15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / pylibdmtx-0.1.10-py2.py3-none-any.whl

Download URL pylibdmtx-0.1.10-py2.py3-none-any.whl
Size 23.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ab73df28b64b3f5e29f22e6937b93967eb77ae7fcd5be381e1142f5a7268eb9e
BLAKE2b-256 checksum
How to use checksums
b3c749c464cbee2a7ee33132db45ee01cf52403ae1c49267d53e735726cee55d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release history Release notifications | RSS feed

This release

0.1.10 This release

3 release files

0.1.9

3 release files

0.1.8

3 release files

0.1.7

3 release files

0.1.6

3 release files

0.1.5

3 release files

0.1.4

3 release files

0.1.3

3 release files

0.1.2

3 release files

0.1.1

3 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page