-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpiql.py
366 lines (331 loc) · 13.3 KB
/
rpiql.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#
# Étiquetteuse Brother-QL
# par Mathieu Légaré <[email protected]>
#
# v2.23.12.17
# ^- Version
# ^^- Year
# ^^- Month
# ^^- Day
# of this realease
# Importations
from PIL import Image, ImageDraw, ImageFont
import getopt, sys, os, time, datetime
import configparser, re, shlex
from datetime import datetime
import textwrap
if os.name == 'nt':
import win32api, win32print
# fonction interne
def loadConfig():
global config
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf-8')
def loadDraw():
global drawRules
drawRules = configparser.ConfigParser()
drawRules.read(drawFile, encoding='utf-8')
treepoem=False
def loadBarcode():
global barcode, treepoem
if(barcode is False):
barcode=True
import treepoem
def debug(text):
if(DEBUG==True):
print("[DEBUG]:", text)
def setCut(w,h):
global cutCrop,drawBanner
w=w + 1
h=h + 1
if(drawBanner and w>cutCrop):
debug("Set cutCrop for Banner orientation")
cutCrop=w
elif(h>=cutCrop):
debug("Set cutCrop for normal orientation")
cutCrop=h
debug("Cut Crop is now "+str(cutCrop))
def getValue(value):
global drawValues
return drawValues.get(value)
def saveDraw():
global config,outFile,outDraw,outImage,cutCrop
debug("Save the draw")
if(cutCrop>0):
if(drawRules['paper']['width']=="0"):
debug("Draw is over, cutting for "+str(cutCrop)+"x"+str(drawHeight))
outImage=outImage.crop((0, 0, cutCrop, drawHeight))
elif(drawRules['paper']['height']=="0"):
debug("Draw is over, cutting for "+str(drawWidth)+"x"+str(cutCrop))
outImage=outImage.crop((0, 0, drawWidth, cutCrop))
if(drawBanner):
debug("Is a banner, rotating output")
outImage=outImage.rotate(270, expand=True)
outImage.save(outFile, format='PNG')
def printDraw():
global config,outCopy
i=1
debug("Printing "+str(outCopy)+" copies")
if(DEBUG!=True):
while( i <= outCopy):
debug("Printing copy #"+str(i)+" from "+str(outCopy)+" copies")
if os.name == 'nt':
os.system('print.bat print.lbx "@output.png"')
else:
os.system('sudo brother_ql -m '+config['printer']['model']+' -p usb://'+config['printer']['usb']+'/'+config['printer']['serial']+' print '+outFile+' -l 62 --600dpi')
time.sleep(2)
i+=1
else:
debug("Send the output draw to printer (disabled when DEBUG is enabled)")
def lineDraw(posx,posy,width,height):
global config,outDraw
debug("Draw a line")
shape = [(posx, posy), (posx+width, posy+height)]
outDraw.line(shape, fill =cBlack, width = 0)
def textDraw(text,posx,posy,width,height,align,overflow):
#postion of text
global config,outDraw,outImage,drawWidth,drawHeight
if(int(width)==0):
width=int(drawWidth - posx)
if(int(height)==0):
height=int(drawHeight - posy)
if(outImage):
debug("Adding text '" + text +"'")
textFont = ImageFont.truetype(config['default']['font'], height)
fontSize=False
if(overflow=="fit"):
debug("Try to fit text into space")
# Text do not overflow
lastFontSize=False
testFontSize=height
while(testFontSize >= 0 and fontSize is False):
testFont = ImageFont.truetype(config['default']['font'], testFontSize)
if(testFont.getbbox(text)[3]<=height and (testFont.getbbox(text)[2]<=width or width==0)):
# Text is lower of the height of space
if((testFont.getbbox(text)[3] >= (height - 2))):
debug("Font size found")
# Text is same of above the limit of height
# Font size found
fontSize=testFontSize
else:
debug("Font size found, but increase it")
# Text can get more space, increase it ?
lastFontSize=testFontSize
testFontSize=testFontSize + 1
else:
# Height or width Overflow
if(lastFontSize):
debug("Use last font size found")
# Previous check is good, the new overflow, use the last good result
# Font size found
fontSize=lastFontSize
else:
debug("Try a smaller font size")
# Size overflow, test a smaller size
testFontSize=testFontSize - 1
debug("Tested font size result: "+str(testFont.getbbox(text)[2])+"x"+str(testFont.getbbox(text)[3]))
elif(overflow=="wrap"):
debug("Try to wrap text")
# Text do not overflow, adding return for paragraph text
#TODO
# Test multi lines, if \r not work, try by cut he total height and split lines
# calc car len, / 2, split by space, count each word added for car len, when over the half, add return
# if do not fit, retry with / 3, / 4 , etc...
#TODO
lines = textwrap.wrap(text, width=width)
draw.multiline_text((x,y), '\n'.join(lines))
fontSize=height #to replace
else:
debug("Do nothing for text overflow")
# past without rework
fontSize=height
textFont = ImageFont.truetype(config['default']['font'], fontSize)
textWidth=textFont.getbbox(text)[2]
textHeight=textFont.getbbox(text)[3]
np=0
if(align=="center"):
debug("Centering text")
# Centering the text
np=width - textWidth
setCut(posx + textWidth + np,posy + textHeight)
posx=posx + round(np / 2,0)
elif(align=="right"):
debug("Text to right")
# Text begin at right
np=width - textWidth
posx=posx + np
setCut(posx + textWidth,posy + textHeight)
else:
debug("Text to left")
# Text degin at left
posx=posx
setCut(posx + textWidth,posy + textHeight)
outDraw.text((posx,posy), str(text), font=textFont, fill=cBlack)
else:
print("*** Can't adding text, image is no created")
def imageDraw(file,posx,posy,width,height):
global config,outDraw,outImage,drawWidth,drawHeight
debug("Open image : "+str(file)+" and try to generate with "+str(width)+"x"+str(height))
if(outImage):
image = Image.open(file).convert("RGBA")
if(int(width)!=0 or int(height)!=0):
if(int(width)!=0 and int(height)==0):
wpercent = (width/float(image.size[0]))
height = round((float(image.size[1])*float(wpercent)),0)
elif(int(width)==0 and int(height)!=0):
hpercent = (height/float(image.size[1]))
width = round((float(image.size[0])*float(hpercent)),0)
debug("Resize image to "+str(width)+"x"+str(height))
image=image.resize((int(width), int(height)))
debug("Drawing image at "+str(posx)+"x"+str(posy)+" with size of "+str(image.size[0])+"x"+str(image.size[1]))
outImage.paste(image, (posx,posy), image)
setCut(posx + width,posy + height)
else:
print("*** Can't adding text, image is no created")
def barcodeDraw(code,posx,posy,width,height,sub):
encode="code128"
global config,outDraw,outImage,drawWidth,drawHeight
debug("Generating barcode")
if(outImage):
image = treepoem.generate_barcode(barcode_type=encode,data=code,)
image.convert("1").save("barcode.jpg")
if(sub=="sub"):
subSize=round(height / 5,0)
imageDraw("barcode.jpg",posx,posy,width,height - subSize)
textDraw(code,posx,int(posy + (height - subSize)),int(width),int(subSize),"center","fit")
else:
imageDraw("barcode.jpg",posx,posy,width,height)
else:
print("*** Can't adding text, image is no created")
def newDraw():
global config,outImage,outDraw,drawRules,drawWidth,drawHeight,drawRotate,drawBanner
if(drawRules):
debug("Create Draw")
drawWidth=int(drawRules['paper']['width'])
drawHeight=int(drawRules['paper']['height'])
drawRotate=int(drawRules['paper']['rotate'])
if(drawWidth == 0):
drawWidth=int(config['default']['paperlength'])
if(drawHeight == 0):
drawHeight=int(config['default']['paperlength'])
if(drawRotate == 1):
drawBanner=True
debug("New draw with dimension of "+str(drawWidth)+"x"+str(drawHeight)+" pixels")
if(drawBanner):
debug("Draw is a banner")
outImage=Image.new('RGBA', (int(drawWidth), int(drawHeight)), color = cWhite)
outDraw=ImageDraw.Draw(outImage)
debug("Draw created")
else:
print("*** No drawing rules loaded")
## Configurations
loadConfig()
drawFile=False
drawRules=False
drawWidth=0
drawHeigh=0
drawRotate=0
drawBanner=False
drawValues={}
cutCrop=0
barcode=False
## Couleurs
cWhite=(255,255,255)
cBlack=(0,0,0)
## Police de caractère
fName=config['default']['font']
## Paramètre de sortie
outDraw=False
outImage=False
outFile="output.png"
outCopy=1
## Mode Debug
if(str(config['default']['debug'])=="True"):
print("Debug Enabled")
DEBUG=True
else:
DEBUG=False
# application
argv = sys.argv[1:]
opts, args = getopt.getopt(argv,"ha:f:c:v:")
help=False
cmd=False
for opt, arg in opts:
if opt == '-h':
help=True
cmd=True
print("*** Help for command parameters")
print("\t-f\t<filepath>\t\tLoading draw file\r\n\t--file\t<filepath>")
print("\t-v\t<id=\"value\":id=\"value\">\tReplace values of id in draw file\r\n\t--value\t<id=\"value\";id=\"value\">")
print("\t-c\t<copies>\t\tPrint number of copies of result\r\n\t--copies\t<copies>")
elif opt == '-f' or opt == "--file":
cmd=True
debug("Loading draw file " + arg)
drawFile=arg
elif opt == '-c' or opt == "--copies":
cmd=True
if(int(arg) >= 1):
debug("Printing " + arg + " copies")
outCopy=int(arg)
else:
debug("Can't set copies to " + str(arg) + ". Must be granter or equal to 1")
elif opt == '-v' or opt == "--values":
cmd=True
debug("Loading draw values " + arg)
#drawValues = arg
items = arg.split(';')
# Récupérer les valeurs des arguments de ligne de commande
for item in items:
# Vérifier si le séparateur '=' est présent dans la chaîne item
if '=' in item:
key, value = item.split('=')
drawValues[key] = value.replace('"','')
else:
#Determine parameters
debug("Unknown parameter '"+ str(opt) + "'.")
if(help is False and cmd is True):
# Help is not diosplayed, running app
if(drawFile):
# Drawing from file
loadDraw()
else:
# Drawing from command line
drawRules=False
# Generating the new draw
newDraw()
# Reading if is a drawFile
if(drawFile):
for section in drawRules.sections():
if section.lower() != 'paper':
# Traitez ici chaque section autre que 'paper'
if(drawRules[section]['type']=="text"):
if(getValue(section) is not None):
value=getValue(section)
else:
value=drawRules[section]['value']
textDraw(str(value),int(drawRules[section]['posx']),int(drawRules[section]['posy']),int(drawRules[section]['width']),int(drawRules[section]['height']),drawRules[section]['align'],drawRules[section]['overflow'])
elif(drawRules[section]['type']=="image"):
if(getValue(section) is not None):
value=getValue(section)
else:
value=drawRules[section]['value']
imageDraw(value,int(drawRules[section]['posx']),int(drawRules[section]['posy']),int(drawRules[section]['width']),int(drawRules[section]['height']))
elif(drawRules[section]['type']=="barcode"):
loadBarcode()
if(getValue(section) is not None):
value=getValue(section)
else:
value=drawRules[section]['value']
barcodeDraw(str(value),int(drawRules[section]['posx']),int(drawRules[section]['posy']),int(drawRules[section]['width']),int(drawRules[section]['height']),drawRules[section]['params'])
elif(drawRules[section]['type']=="line"):
lineDraw(int(drawRules[section]['posx']),int(drawRules[section]['posy']),int(drawRules[section]['width']),int(drawRules[section]['height']))
else:
print("*** Type unknow of entry")
# Save the draw
saveDraw()
# Send to printer
printDraw()
elif(cmd is False):
print("Missing a valid parameter. Try '-h' for help.")
# Exiting the app
sys.exit()