Skip to main content

Fork of NaturalHistoryMuseum pylibdmtx with GS1 extensions

Project description

This is a fork of [pylibdmtx](https://github.com/NaturalHistoryMuseum/pylibdmtx/) that adds the capability to extract extra information from decoder to grade the quality of data matrix barcodes.

Key changes:

  • Introduce _pixels_fit_and_raw_data function to extract the fit and raw array from decoder.

  • Introduce dmtxDecodeGetPixelValue in wrapper.py to extract details on fit and raw arrays.

  • Introduce sampling_rate argument in decode() function, to control th size of fit and raw arrays.

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/actions/workflow/status/NaturalHistoryMuseum/pylibdmtx/test.yml?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

To use the read_datamatrix and write_datamatrix command-line scripts, you will also need to install Pillow >= 3.2.0:

pip install "Pillow>=3.2.0"

If you want to install the package from PyPI:

pip install pylibdmtx-gs1grader

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylibdmtx-gs1grader-0.1.11.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylibdmtx_gs1grader-0.1.11-py2.py3-none-any.whl (25.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pylibdmtx-gs1grader-0.1.11.tar.gz.

File metadata

  • Download URL: pylibdmtx-gs1grader-0.1.11.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for pylibdmtx-gs1grader-0.1.11.tar.gz
Algorithm Hash digest
SHA256 1859af124f7649e85d582c2dd8a5cd3d5c66dde110d799c0168b30403cada1dd
MD5 69ac82ddec34394f94ff12cce1d18abd
BLAKE2b-256 8a720fa9a48da945abe1241d407465b01537463a598181ff936c8d63b830e0cd

See more details on using hashes here.

File details

Details for the file pylibdmtx_gs1grader-0.1.11-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pylibdmtx_gs1grader-0.1.11-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c7b10c2ccd8f966cd7accefc86119cf0e5fbe337dbd3000f785ac3b058715ac1
MD5 32d0f28345f6602634954432edb69ec7
BLAKE2b-256 fea0d22ccaf76726d75e2cee21953a70fcd720acf725483c4120f760d6976898

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page