...

/

Reading and Writing Data Matrix Codes

Reading and Writing Data Matrix Codes

Learn how to read and write Data Matrix barcodes using the pylibdmtx library for Python.

Writing data to the Data Matrix

To encode data into a Data Matrix barcode, we can do as in the following example:

Press + to interact
from pylibdmtx.pylibdmtx import encode
from PIL import Image, ImageDraw
data = '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 ...