Skip to content

Instantly share code, notes, and snippets.

@Chaz6
Created January 17, 2025 09:25
Show Gist options
  • Save Chaz6/3ea7167ce74cead28389f75ac4355095 to your computer and use it in GitHub Desktop.
Save Chaz6/3ea7167ce74cead28389f75ac4355095 to your computer and use it in GitHub Desktop.
make-datamatrix-shortuuid-label-v2.py
#!/usr/bin/env python3
import argparse
import shortuuid
from itertools import batched
from pylibdmtx.pylibdmtx import encode
padding = 5
output_svg_width = 25
output_svg_height = 25
output_svg_units = "mm"
bpp = 3 # int(dmtx.bpp / 8)
def make_svg():
uuid = shortuuid.uuid()
dmtx = encode(uuid.encode("utf8"))
h = dmtx.height
w = dmtx.width
# SVG header
data = f'<svg width="{output_svg_width}{output_svg_units}" height="{output_svg_height}{output_svg_units}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">'
pixels_x = len(dmtx.pixels) / bpp / w / padding
pixels_y = len(dmtx.pixels) / bpp / h / padding
x_i = output_svg_width / pixels_x
y_i = output_svg_height / pixels_y
y = -1 * y_i
for row in batched(dmtx.pixels, w * bpp * padding):
y += y_i
x = -1 * x_i
for column in batched(row[: w * bpp], bpp * padding):
x += x_i
if not column[0]:
data += f'<rect x="{x}{output_svg_units}" y="{y}{output_svg_units}" width="{x_i}{output_svg_units}" height="{y_i}{output_svg_units}" style="fill:#000000" />'
# SVG footer
data += "</svg>"
with open(str(uuid) + ".svg", "w") as f:
f.write(data)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("qty")
args = parser.parse_args()
for _ in range(int(args.qty)):
make_svg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment