pylibdmtx

repository·master·Indexed 41 days ago

https://github.com/naturalhistorymuseum/pylibdmtx

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

Tokens
1.5K
Snippets
7
Records
9
Agent score
2%

What's inside pylibdmtx

  1. Install pylibdmtx

    master

    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:

    • macOS: brew install libdmtx
    • Linux: sudo apt-get install libdmtx0a

    Windows users have the libdmtx DLLs included in the Python wheels.

    pip install git+https://github.com/NaturalHistoryMuseum/pylibdmtx.git
  2. Set up a development environment for pylibdmtx

    master

    To 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.png
  3. Build and release pylibdmtx to PyPI

    master

    The release process involves building source and wheel distributions for different platforms, ensuring the correct libdmtx.dll is included in each, and uploading via twine.

    1. Build Wheels

    To build for Windows, you must swap the MANIFEST.in content between win32 and win64 definitions to prevent cross-pollination of DLLs.

    2. Test on TestPyPI

    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.

    3. Final Release

    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/*
  4. Configure libdmtx.dll for Windows development

    master

    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:

    • 32-bit libdmtx.dll $\rightarrow$ libdmtx-32.dll
    • 64-bit libdmtx.dll $\rightarrow$ libdmtx-64.dll
  5. Encode Data Matrix barcodes with encode()

    master

    The 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')
  6. Decode Data Matrix barcodes with decode()

    master

    The decode function is used to read Data Matrix barcodes from various image formats. It supports:

    • PIL.Image instances
    • numpy.ndarray (e.g., from OpenCV)
    • Raw byte tuples in the format (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))