-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarManual.py
110 lines (88 loc) · 3.57 KB
/
barManual.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
""" methods to interface to brother label makers and print 1D barcodes """
try:
import Image, ImageDraw, ImageFont
except ImportError:
try:
from PIL import Image, ImageDraw, ImageFont # lint:ok
except ImportError:
import sys
sys.stderr.write('PIL not found. Image output disabled.\n\n')
Image = ImageDraw = ImageFont = None # lint:ok
import barcode
from barcode.writer import ImageWriter
import os
import subprocess
class fake():
pass
defaultPrinter = os.getenv('PRINTER_MODEL')
if defaultPrinter is None: raise Exception("enviroment variable for defaultPrinter not found")
defaultPort = os.getenv('PRINTER_PORT')
if defaultPort is None: raise Exception("enviroment variable for defaultPort not found")
defaultLabel = os.getenv('PRINTER_LABEL')
if defaultLabel is None: raise Exception("enviroment variable for defaultLabel not found")
def printLabel(pngName,printerName=defaultPrinter,printerPort=defaultPort,labelSize = defaultLabel):
subprocess.call(['brother_ql','-bpyusb' ,
'-m'+printerName,
'-p'+printerPort,
'print',
'-l'+labelSize,
pngName])
def px2mm(px,dpi=300):
return px*25.4/dpi
def mm2px(mm, dpi=300):
return int((mm * dpi) / 25.4)
#create a singe image with two barcodes on it.
#sized for individual labels
def makeTag(serialNum,assetTag,outputfile):
""" create a single image with two barcodes in it
sized for individual labels, 62mmx28mm
"""
if (serialNum is None) and (assetTag is None):
raise Exception("must provide at least one value for label")
code39 = barcode.get_barcode_class('code39')
twoBarcodes = (serialNum is not None) and (assetTag is not None)
wrt=ImageWriter()
label = (696,271) #pixels for a 62mmx28mm label
margin = 3 #mm
width = px2mm(label[0])-2*margin #showable width in mm
if twoBarcodes:
modHeight=7 # bardcode height
else:
modHeight=14
#code39 5 bars, 4 spaces per symbol. 3 wide, 6 narrow, 3:1 ratio
#settings for the Serial number
#resize the width of a line to make them fit in the printable width
#16 modules per symbol
if serialNum is not None:
a= code39(serialNum,add_checksum=False)
wrt.set_options({'text':'SN: '+a.code,'text_distance':0.5,
'quiet_zone':0,'module_height':modHeight,
'module_width':width/((2+len(a.get_fullcode()))*16)})
apil = wrt.render(a.build())
else:
apil = fake()
apil.size=(0,0)
if assetTag is not None:
b= code39(assetTag,add_checksum=False)
#settings for the Asset Tag
wrt.set_options({'text':'TAG: '+b.code,'text_distance':0.5,
'quiet_zone':0,'module_height':modHeight,
'module_width':width/((2+len(b.get_fullcode()))*16)})
bpil = wrt.render(b.build())
else:
bpil = fake()
bpil.size = (0,0)
#print (apil.size)
#print (bpil.size)
if (apil.size[1]+bpil.size[1])>label[1]: raise Exception("images dont fit")
#create a custom canvas of the correct size
#paste both barcodes into it, aproximately centered
im = Image.new('RGB',label,'white')
top = int((label[1]-(apil.size[1]+bpil.size[1]))/2)
left = int((label[0]-apil.size[0])/2)
if serialNum is not None:
im.paste(apil,(0+left,top,apil.size[0]+left,top+apil.size[1]))
left = int((label[0]-bpil.size[0])/2)
if assetTag is not None:
im.paste(bpil,(0+left,top+apil.size[1],bpil.size[0]+left,top+apil.size[1]+bpil.size[1]))
im.save(outputfile,'PNG')