Commit d93b45ea authored by Hugo Kerstens's avatar Hugo Kerstens

Use pylibdmtx for datamatrix generation

parent 4606b8fe
......@@ -22,7 +22,6 @@ pdfrw
reportlab
Wand
Pillow # also scan processing
pyStrich # TODO: can we replace this with stuff from pylibdmtx?
# Scan processing
opencv-python
......
from io import BytesIO
from tempfile import NamedTemporaryFile
import PIL
from pdfrw import PdfReader, PdfWriter, PageMerge
from pystrich.datamatrix import DataMatrixEncoder
from pylibdmtx.pylibdmtx import encode
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
......@@ -16,9 +15,9 @@ def generate_pdfs(exam_pdf_file, exam_id, copy_nums, output_paths, id_grid_x,
"""
Generate the final PDFs from the original exam PDF.
To maintain a consistent size of the DataMatrix codes, adhere to (# of
letters in exam ID) + 2 * (# of digits in exam ID) = C for a certain
constant C. The reason for this is that pyStrich encodes two digits in as
To maintain a consistent grid size of the DataMatrix codes, adhere to
(# of letters in exam ID) + 2 * (# of digits in exam ID) = C for a certain
constant C. The reason for this is that libdmtx encodes two digits in as
much space as one letter.
If maximum interchangeability with version 1 QR codes is desired (error
......@@ -155,9 +154,9 @@ def generate_datamatrix(exam_id, page_num, copy_num):
"""
Generates a DataMatrix code to be used on a page.
To maintain a consistent size of the DataMatrix codes, adhere to (# of
letters in exam ID) + 2 * (# of digits in exam ID) = C for a certain
constant C. The reason for this is that pyStrich encodes two digits in as
To maintain a consistent grid size of the DataMatrix codes, adhere to
(# of letters in exam ID) + 2 * (# of digits in exam ID) = C for a certain
constant C. The reason for this is that pylibdmtx encodes two digits in as
much space as one letter.
If maximum interchangeability with version 1 QR codes is desired (error
......@@ -182,8 +181,10 @@ def generate_datamatrix(exam_id, page_num, copy_num):
data = f'{exam_id}/{copy_num:04d}/{page_num:02d}'
image_bytes = DataMatrixEncoder(data).get_imagedata(cellsize=2)
return PIL.Image.open(BytesIO(image_bytes))
encoded = encode(data.encode('utf-8'), size='18x18')
datamatrix = PIL.Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
datamatrix = datamatrix.resize((44, 44)).convert('L')
return datamatrix
def _generate_overlay(canv, pagesize, exam_id, copy_num, num_pages, id_grid_x,
......@@ -192,9 +193,9 @@ def _generate_overlay(canv, pagesize, exam_id, copy_num, num_pages, id_grid_x,
Generates an overlay ('watermark') PDF, which can then be overlaid onto
the exam PDF.
To maintain a consistent size of the DataMatrix codes in the overlay,
To maintain a consistent grid size of the DataMatrix codes in the overlay,
adhere to (# of letters in exam ID) + 2 * (# of digits in exam ID) = C for
a certain constant C. The reason for this is that pyStrich encodes two
a certain constant C. The reason for this is that pylibdmtx encodes two
digits in as much space as one letter.
If maximum interchangeability with version 1 QR codes is desired (error
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment