Code Monkey home page Code Monkey logo

ppf.datamatrix's Introduction

ppf.datamatrix logo

pypi downloads/month

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:

IPython integration

Using the DataMatrix object, you get the SVG source like this:

myDataMatrix.svg()

'<?xml version="1.0" encoding="utf-8" ?><svg ...'

Test! DataMatrix

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

Test! DataMatrix in red on blue background

Note: This sets the colors of the SVG. It does not change the color of the representation inside your IPython terminal.

Advanced Features

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.

Installation

ppf.datamatrix is available via pypi:

pip install ppf.datamatrix

Still reading?

If 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?

Contributing

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.

Change Log

  • 0.2: Fixed RTA problems causing erroneous datamatrices; added capability to specify encoding(s) to use
  • 0.1.2: Fixed bug in RS correction data for each block
  • 0.1.1: Fixed bug in datamatrix.ascii encoding of digit pairs
  • 0.1: Initial port of datamatrix–svg

ppf.datamatrix's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ppf.datamatrix's Issues

Add viewbox to allow to scale via CSS

Hello,

the SVG cannot be scaled via CSS since the path is absolute. If you add viewBox="0 0 W H" (e.g. viewBox="0 0 18 18" if the width and height is 18), scaling works very well.

SVG not working in Openscad or FreeCAD

Hello,
I have almost no knowledge in SVG.
I want to 3D print Datamatrix barcodes.
So the plan was to generate SVGs and then use them in FreeCAD or openscad to extrude them into 3D objects.
But in FreeCAD it shows up as horizontal lines and in openscad, it looks almost like a datamatrix, but it isn't.
I opened the file in Inkscape and it works fine there.

I guess this is a limitation of how the file is generated. Can you maybe explain why? Can this be changed? etc.
Here is the genrated SVG
letters
And here the openscad extrude
openscad

6 digit code results in 12x12 data matrix instead of 10x10

I am doing a project for which I want to generate data-matrixes of 10x10, which stores six integers. In the webdemo, six digits results in a 10x10 data-matrix, but in this ported package, it returns a 12x12 data matrix. Do you perhaps have a fix for this? Thanks!

Issue when importing in Flask this: from ppf.datamatrix import DataMatrix

Hi Adrian,
I really appreciated how you built the package. I tested on Python Console of my Mac OS and it works fine.

The issue come when I want to use it in my flask app by importing this: from ppf.datamatrix import DataMatrix , Flask pop up the error: NO MODULE FOUND named ppf.

Why did you put a dot and really, how can I do to fix that. I'm stuck since your last release of this code.

Tthanks

Encoding invalid characters

Trying to encode a character which is not supported by a codec will raise an exception (=> good). However, the error message is a bit misleading in some cases.

Example:

'ä'.encode('datamatrix.EDIFACT')

UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 0: ordinal not in range(128)
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
/InteractiveInput-1 in <module>
----> 1 'ä'.encode('datamatrix.EDIFACT')

~/Projects/ppf.datamatrix/src/ppf/datamatrix/codec_edifact.py in encode_to_edifact(msg)
     46     elif n_rest == 1:
     47         if len(msg) < 2:
---> 48             enc = (b'\xF0' + pack(msg.encode('ascii'), b'\x1F\x00\x00'))
     49         else:
     50             enc = (b'\xF0' +

UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 0: ordinal not in range(128)

Note that we are trying to encode in 'datamatrix.EDIFACT'. But the error message says there is a failure when encoding to 'ascii'. We should catch this exception and re-raise an exception that say 'ä' is an invalid datamatrix.EDIFACT character.

Similar things happen in other codecs.

Wrong message in DataMatrix

Example code:

from ppf.datamatrix import DataMatrix
from cairosvg import svg2png
from IPython.display import Image, display

s = 'SD0000-REF-C0-I0'
myDataMatrix = DataMatrix(s).svg()
svg = svg2png(myDataMatrix,write_to=None,output_width=100,output_height=100)
display(Image(svg))

Result:
image

This image decodes to "SD0000-REF-C0-I0 U03" and some non-printable characters.

Expected result (from demo https://datalog.github.io/demo/datamatrix-svg/):
datamatrix-20230518-072157
Which decodes correctly, with any message.

I noticed that a "small" change in message results in the correct datamatrix - 'SD0000-R-C0-I0' works well.

Rectangular Datamatrix with garabage at the end are generated for short texts

Firstly, thank you for this excellent library. While attempting to implement a datamatrix code in labelprinterkit with this library, I encountered an issue.

This is a small example code:

from ppf.datamatrix import DataMatrix
dm = DataMatrix('Hello World!', rect=True)
with open('/tmp/test.svg', 'w') as fh:
    fh.write(dm.svg())

I am using the current develop branch (a9e292c) on Python 3.11.4.

The code above gives this result:
defect datamatrix code

My barcode reader is giving the following output: "Hello World!gmlqqy[)>�06���". As you can see, there is some garbage at the end of the line.

Generating the same string with the tools from https://www.bcgen.com/datamatrix-barcode-creator.html gives a bit different output:
working datamatrix code

Which is read fine by my barcode reader.

I don't know if this is a bug or if something is broken in my setup. Maybe someone can try to confirm this issue?

Wrong Code Generation

DataMatrix class does not provide a valid code

Steps to Reproduce

data = "B|BID|54C64-234F1"
myDataMatrix = DataMatrix(data)

Expected Behavior

barcode

Current Behavior

bitmap

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.