-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchlabel.py
75 lines (59 loc) · 2.89 KB
/
batchlabel.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
import os
import argparse
import csv
import ptprint
trenner64 = 'inline:data:image/pbm;base64,UDQKNjQgMQqAAAAAAAAAAQo='
def createLabel(row1, row2, row3, length, name):
if length == 1:
lengthPx = 120
elif length == 2 | length == 3:
lengthPx = 235
else:
print('Invalid label size')
sys.exit()
# Einzelne Schriftzuege erstellen
os.system('convert +antialias -background white -fill black -size x24 -gravity center -bordercolor black -border 1 -font /usr/share/fonts/truetype/msttcorefonts/arialbd.ttf -pointsize 22 label:"' + row1 + '" top.pbm')
if row2 != '':
os.system('convert +antialias -background white -fill black -size x20 -gravity center -bordercolor black -border 0 -trim -font /usr/share/fonts/truetype/msttcorefonts/arialbd.ttf -pointsize 20 label:"' + row2 + '" mid.pbm')
if row3 != '':
os.system('convert +antialias -background white -fill black -size x20 -gravity center -bordercolor black -border 0 -trim -font /usr/share/fonts/truetype/msttcorefonts/arialbd.ttf -pointsize 20 label:"' + row3 + '" bot.pbm')
os.system('convert -size ' +str(lengthPx)+ 'x64 canvas:white canvas.pbm') # Canvas erstellen
if os.path.exists('mid.pbm'):
os.system('composite -gravity center -geometry +0+7 mid.pbm canvas.pbm canvas.pbm') # Mittelteil drauf
if os.path.exists('bot.pbm'):
os.system('composite -gravity south bot.pbm canvas.pbm canvas.pbm') # Unterer Teil drauf
os.system('composite -gravity north top.pbm canvas.pbm canvas.pbm') # Oberer Teil drauf
os.system('convert -rotate 90 canvas.pbm "'+name+'"') # drehen und umbenennen
# aufraeumen
os.system('rm top.pbm')
if os.path.exists('bot.pbm'):
os.system('rm bot.pbm')
if os.path.exists('mid.pbm'):
os.system('rm mid.pbm')
os.system('rm canvas.pbm')
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input-csv', action='store', default='input.csv',
help='File name of the .csv file with the label data. Needs to have 3 columns. Default: input.csv')
parser.add_argument('-s', '--size', action='store', default='3',
help='Length of the label. 1 fits for ESD boxes No. 1. 2 and 3 for Boxes No. 2 and 3. Default: 3')
parser.add_argument('-p', '--printfile', action='store_true',
help='Generates a .prn file for brother prtiners from the generated bitmap')
args = parser.parse_args()
# Label creation from csv
with open(args.input_csv, 'rb') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
i=1
for row in csvreader:
createLabel(row[0], row[1], row[2], int(args.size), str(i)+'.pbm')
i = i+1
# Merge the labels
os.system('mv 1.pbm output.pbm')
if i > 1:
for j in xrange(2, i):
os.system('convert output.pbm "' + trenner64 + '" ' + str(j) + '.pbm -append output.pbm')
os.system('rm ' + str(j) + '.pbm') # aufraeumen
# Generate printfile
if args.printfile:
ptData = ptprint.PtData(open('output.pbm', 'rb').read())
ptData.create()
open('printfile.prn', 'wb').write(ptData.getData())