Reading and Writing Data Matrix Codes
Learn how to read and write Data Matrix barcodes using the pylibdmtx library for Python.
We'll cover the following...
We'll cover the following...
Writing data to the Data Matrix
To encode data into a Data Matrix barcode, we can do as in the following example:
Python 3.10.4
from pylibdmtx.pylibdmtx import encodefrom PIL import Image, ImageDrawdata = 'educative'.encode('utf8')encoded = encode(data)img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)img.save('output/dmtx.png')
Lines 1–2: First, we import the encode() from the pylibdmtx library and the Image and ImageDraw classes from the Pillow library.
Line 4: Then, we can encode the data. For this example, we’ll encode the name of our course platform, educative. From the documentation of the library, it seems to be recommended to encode the data string as utf-8. Nevertheless, we could experiment with other encodings here.
Lines 6–7: ...