Sana
Sana

Reputation: 1

Data Matrix GS1 in python

help me please( I can't generate DataMatrix with GS1 symbol(FNC1) in python. when I just try the below code I can't get symbol gs1 in the screen

from ppf.datamatrix import DataMatrix
data = '0104680059740223213(OkNQCgC*qXF91KZF092gSnuwHu7c3KkrgPhR7eWzfH5/Qg=gSnuwHu7c3KkrgPhR7eWzfH5/Qg=gSnuwHu7c3KkrgPhR7eWzfH5/Qg=gSnu'
myDataMatrix = DataMatrix(data)
myDataMatrix

enter image description here

Upvotes: 0

Views: 2233

Answers (3)

bjallamo
bjallamo

Reputation: 21

The solution is using a library which supports GS1 DataMatrix codes and then create a file out of it.

The one for you is treepoem, which identifies the values, adds FNC1 as a starting character and GS characters where needed.

Here is a simple script to generate correctly, you might have to adjust the data content and options to your needs.

from treepoem import generate_barcode
from PIL import Image

def generate_and_print(gtin, serial_number, expiry_date, batch_number):
    # Generate datamatrix
    datamatrix = generate_barcode(
        barcode_type='gs1datamatrix',
        data=f"(01){gtin}(21){serial_number}(17){expiry_date}(10){batch_number}",
        options={"parsefnc": True, "format": "square", "version": "26x26"})
    
    # Resize datamatrix to desired size
    dm_size_px = (120, 120)
    datamatrix = datamatrix.resize(dm_size_px, Image.NEAREST)

    # Create white picture
    picture_size_px = (200, 200)
    picture = Image.new('L', picture_size_px, color='white')

    # Position the datamatrix
    barcode_position_px = (40, 40)
    picture.paste(datamatrix, barcode_position_px)

    # Save the image
    picture.save("datamatrix.png")

gtin = "01234567890128"
serial_number = "01234567891011"
expiry_date = "250731"
batch_number = "DATAMATRIXTEST"

generate_and_print(gtin, serial_number, expiry_date, batch_number)

Upvotes: 1

Shengmin
Shengmin

Reputation: 1

You might have to find the encoder in the module you used and add chr(232) which is the FNC1 symbol at the beginning of the codewords. Then all of your datamatrix will be in GS1 format.

Upvotes: 0

Petr Blahos
Petr Blahos

Reputation: 2433

You are printing out the DataMatrix object. There is no reason for an image to show up. The call:

myDataMatrix.svg()

will return the content of the svg file with the datamatrix. You can save the contents into a file and view it, for example in a web browser.

with open("myfilename.svg", "w") as f:
    f.write(myDataMatrix.svg())

If you are refering to the ppf.datamatrix homepage, which seems to suggest, that executing myDataMatrix should somehow print out the code: They probably use Jupyter notebook ot ipython.

Upvotes: 0

Related Questions