forked from Juerd/shalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode.py
executable file
·130 lines (93 loc) · 4.37 KB
/
barcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python3 -u
import os, sys, io, subprocess, glob, tempfile
from PIL import Image, ImageFont, ImageDraw
PRINTER_GLOB = '/dev/usb/lp0'
def generate_aztec(txt):
logo = Image.open('why-logo-24mm.png')
logo = logo.convert('L').point( lambda p: 255 if p > 150 else 0 ).convert('1')
zint = subprocess.run(['zint', '--barcode', '92', '--vers', '8', '--scale', '2', '--direct', '--data', txt], capture_output=True)
if zint.returncode != 0:
print(f'Zint error. Return code: \033[96m\033[1m{zint.returncode}\033[0m', file=sys.stderr)
print(f'\nStdErr: {zint.stderr}\nStdOut: {zint.stdout}', file=sys.stderr)
os.exit(101)
barcode = Image.open(io.BytesIO(zint.stdout))
im = Image.new('1', (128, 214))
im.paste(1, (0, 0) + im.size)
im.paste(logo, ((im.size[0]-logo.size[0])//2, 0))
im.paste(barcode, ((im.size[0]-barcode.size[0])//2, logo.size[1] + 2))
draw = ImageDraw.Draw(im)
firacode = ImageFont.truetype('FiraCode-Medium.ttf', size=30)
while '\n' not in txt and draw.textlength(txt, firacode) > 124:
firacode = ImageFont.truetype('FiraCode-Medium.ttf', size=firacode.size*0.99)
draw.text((im.size[0]/2, logo.size[1]+barcode.size[1] + 6), txt, fill=0, font=firacode, anchor='ma')
im = im.transpose(Image.Transpose.ROTATE_90)
return im
def generate_code128(txt):
logo = Image.open('why-logo-12mm.png')
logo = logo.convert('L').point( lambda p: 255 if p > 150 else 0 ).convert('1')
zint = subprocess.run(['zint', '--barcode', '20', '--scale', '0.5', '--height', '48', '--direct', '--notext', '--data', txt], capture_output=True)
if zint.returncode != 0:
print(f'Zint error. Return code: \033[96m\033[1m{zint.returncode}\033[0m', file=sys.stderr)
print(f'\nStdErr: {zint.stderr}\nStdOut: {zint.stdout}', file=sys.stderr)
os.exit(102)
barcode = Image.open(io.BytesIO(zint.stdout))
im = Image.new('1', (logo.size[0] + 4 + barcode.size[0], 128))
im.paste(1, (0, 0) + im.size)
im.paste(logo, (0, ((im.size[1]-logo.size[1])//2)))
im.paste(barcode, (logo.size[0] + 2, 29))
draw = ImageDraw.Draw(im)
firacode = ImageFont.truetype('FiraCode-Medium.ttf', size=18)
draw.text((logo.size[0] + 4 + barcode.size[0]/2, 29 + barcode.size[1] + 1), txt, fill=0, font=firacode, anchor='ma')
return im
def get_label_size(printer):
with open(printer, 'r+b') as fp:
buf = io.BytesIO()
fp.write(b'\x00'*200+b'\x1b\x40\x1b\x69\x53')
while len(buf.getvalue()) < 32:
buf.write(fp.read())
return int(buf.getvalue()[10])
if __name__ == '__main__':
debug = False
if '-d' in sys.argv:
debug = True
sys.argv.remove('-d')
if len(sys.argv) < 3 or sys.argv[1] not in ['aztec', 'code128']:
print(f'Usage: {sys.argv[0]} [-d] {{aztec|code128}} <text> ...')
print(f' -d display (instead of print) the barcode')
print(f' aztec square 2d barcode')
print(f' code128 long 1d barcode')
print(f' <text> text to be encoded in the barcode')
sys.exit(1)
printers = glob.glob(PRINTER_GLOB)
if len(printers) < 1:
print(f'Printer not found: no matches for \033[96m\033[1m{PRINTER_GLOB}\033[0m', file=sys.stderr)
print('Continuing for debug') if debug else sys.exit(2)
else:
printer = printers[0]
if len(printers) > 1:
print(f'{len(printers)} printers found, picking \033[96m\033[1m{printer}\033[0m')
label_size = get_label_size(printer)
if label_size == 0:
print('Missing label tape', file=sys.stderr)
print('Continuing for debug') if debug else sys.exit(3)
if sys.argv[1] == 'aztec' and (debug or label_size >= 18):
generate_label = generate_aztec
elif sys.argv[1] == 'code128' and (debug or label_size == 12):
generate_label = generate_code128
else:
print(f'Cannot print {sys.argv[1]} code on \033[96m\033[1m{label_size}mm\033[0m tape.', file=sys.stderr)
sys.exit(4)
imgs = []
for txt in sys.argv[2:]:
im = generate_label(txt)
tmp = tempfile.NamedTemporaryFile()
im.save(tmp, 'PPM')
imgs.append(tmp)
if debug:
im.show()
if not debug:
ptouch = subprocess.run(['./ptouch-770/ptouch-770-write', '0'] + [i.name for i in imgs], capture_output=True)
if ptouch.returncode != 0:
print(f'ptouch-770 error. Return code: \033[96m\033[1m{ptouch.returncode}\033[0m', file=sys.stderr)
print(f'StdErr: {ptouch.stderr.decode("ascii")}\nStdOut: {ptouch.stdout.decode("ascii")}', file=sys.stderr)
sys.exit(5)