ppf.datamatrix is a pure-python package to generate datamatrix codes in SVG. Also, it integrates nicely in IPython.
ppf.datamatrix has been ported from datalog's datamatrix-svg, which is written in javascript. If you like to see what you'll get before installation, check out their nice web demo.
Creating a datamatrix with ppf.datamatrix is as easy as
>>> from ppf.datamatrix import DataMatrix
>>> myDataMatrix = DataMatrix('Test!')If you are working in a graphically enabled IPython terminal, you'll see your datamatrix immediately:
Using the DataMatrix object, you get the SVG source like this:
>>> myDataMatrix.svg() # doctest: +ELLIPSIS
'<?xml version="1.0" encoding="utf-8"?>...<path d="M0,0.5 h1m1,0h1m1,...'Use this on your website, to stamp a pdf, to uniquely identify a drawing, or whatever you like. Background and foreground color are configurable by specifying fg and/or bg arguments. Create a light blue matrix on a petrol background like this:
>>> myDataMatrix.svg(fg='#EEF', bg='#09D') # doctest: +SKIPNote: This sets the colors of the SVG. It does not change the color of the representation inside your IPython terminal.
Furthermore, you can tweak the SVG output: By default, ppf.datamatrix creates
very compact SVG code suitable for most uses. However, if you want to use the
datamatrix code for, e.g., laser engraving, you might need an SVG that draws
every cell of the matrix as a closed shape, such as a rectangle:
>>> print(myDataMatrix.svg(geom='rect')) # doctest: +ELLIPSIS
<?xml version="1.0" encoding="utf-8"?><svg baseProfile="tiny" version="1.2" viewBox="-1 -1 14 14" width="14mm" height="14mm" style="background-color:#FFF" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect width="1" height="1" x="0" y="0" fill="#000"/>
<rect width="1" height="1" x="2" y="0" fill="#000"/>
<rect width="1" height="1" x="4" y="0" fill="#000"/>
...When viewed on screen, there are no visible differences between the default and the 'rect' geometry. You can the specify the margin and the (physical) size of the datamatrix cells as well, check the documentation.
ppf.datamatrix supports a variety of encodings, namely EDIFACT ('datamatrix.edifact'), ASCII ('datamatrix.ascii'), X12 ('datamatrix.X12'), C40 ('datamatrix.C40'), TEXT ('datamatrix.text'). These are used to store your message inside the datamatrix code efficiently. DataMatrix handles the encoding internally: If you just want to create a DataMatrix, you don't have to care about any of this. If you want to do advanced stuff (designing your own form of matrix code, maybe), ppf.datamatrix enables you to use its encoders. After importing ppf.datamatrix, they are available via the python codecs system:
import ppf.datamatrix
encoded = 'TEST'.encode('datamatrix.edifact')
encoded
b'\xf0PT\xd4'
decoded = encoded.decode('datamatrix.edifact')
decoded
'TEST'Furthermore it is possible to tell the DataMatrix class which codecs to use. The default is to try all valid datamatrix codecs and select the one resulting in the shortest code. This line:
>>> myDataMatrix = DataMatrix('Test!', codecs=['C40', 'edifact'])will try (only) datamatrix.C40 and datamatrix.edifact and select the shorter one. Of course, you can provide a list of one to enforce a particular encoding.
ppf.datamatrix is available via pypi:
pip install ppf.datamatrixIf you read this far, you're probably not here for the first time. If you use and like this project, would you consider giving it a Github Star? (The button is at the top of this website.) If not, maybe you're interested in one of my other projects?
Did you find a bug and would like to report it? Or maybe you've fixed it already or want to help fixing it? That's great! Please read CONTRIBUTING to learn how to proceed from there.
To help ascertain that contributing to this project is a pleasant experience, we have established a code of conduct. You can expect everyone to adhere to it, just make sure you do as well.
- Breaking Changes:
- SVG: Add viewBox
- SVG: Top-left cell of datamatrix now at (0, 0) instead of (1, 1)
- SVG: Tidy-up white space
- New Features:
- SVG: Option to set margin and (physical) cell size
- SVG: Option to use 'rect' geometry for, e.g, laser engraving
- New Features:
- added capability to specify encoding(s) to use
- Bug Fixes:
- Fixed RTA problems causing erroneous datamatrices
- Bug Fixes:
- Fixed bug in RS correction data for each block
- Bug Fixes:
- Fixed bug in datamatrix.ascii encoding of digit pairs
- Initial port of datamatrix–svg
