Skip to content

Commit

Permalink
improve log level handling; print multiple labels; fix Pillow depreca…
Browse files Browse the repository at this point in the history
…tions

* The log level has not been converted correctly when passed from the terminal.
* Add support for printing multiple labels with the same content.
* Fix `Pillow` deprecation warnings. Changes require `Pillow>=8.0.0`. Reference: https://pillow.readthedocs.io/en/stable/deprecations.html#font-size-and-offset-methods
  • Loading branch information
FriedrichFroebel committed Apr 16, 2023
1 parent 9e20b6d commit 17dbdf1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
20 changes: 13 additions & 7 deletions brother_ql_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys, logging, random, json, argparse
from io import BytesIO

import bottle
from bottle import run, route, get, post, response, request, jinja2_view as view, static_file, redirect
from PIL import Image, ImageDraw, ImageFont

Expand Down Expand Up @@ -70,6 +71,7 @@ def get_label_context(request):
'margin_bottom': float(d.get('margin_bottom', 45))/100.,
'margin_left': float(d.get('margin_left', 35))/100.,
'margin_right': float(d.get('margin_right', 35))/100.,
'label_count': int(d.get('label_count', 1)),
}
context['margin_top'] = int(context['font_size']*context['margin_top'])
context['margin_bottom'] = int(context['font_size']*context['margin_bottom'])
Expand Down Expand Up @@ -116,8 +118,9 @@ def create_label_im(text, **kwargs):
if line == '': line = ' '
lines.append(line)
text = '\n'.join(lines)
linesize = im_font.getsize(text)
textsize = draw.multiline_textsize(text, font=im_font)
# linesize = im_font.getsize(text)
left, top, right, bottom = draw.multiline_textbbox(xy=(0, 0), text=text, font=im_font)
textsize = (right - left, bottom - top)
width, height = kwargs['width'], kwargs['height']
if kwargs['orientation'] == 'standard':
if label_type in (ENDLESS_LABEL,):
Expand Down Expand Up @@ -190,7 +193,8 @@ def print_text():
return return_dict

im = create_label_im(**context)
if DEBUG: im.save('sample-out.png')
if bottle.DEBUG:
im.save('sample-out.png')

if context['kind'] == ENDLESS_LABEL:
rotate = 0 if context['orientation'] == 'standard' else 90
Expand All @@ -203,10 +207,12 @@ def print_text():
red = True
create_label(qlr, im, context['label_size'], red=red, threshold=context['threshold'], cut=True, rotate=rotate)

if not DEBUG:
if not bottle.DEBUG:
try:
be = BACKEND_CLASS(CONFIG['PRINTER']['PRINTER'])
be.write(qlr.data)
for i in range(context['label_count']):
logger.info('Printing label %d of %d ...', i, context['label_count'])
be.write(qlr.data)
be.dispose()
del be
except Exception as e:
Expand Down Expand Up @@ -243,7 +249,8 @@ def main():
else:
LOGLEVEL = CONFIG['SERVER']['LOGLEVEL']

if LOGLEVEL == 'DEBUG':
# LOGLEVEL will be numeric if parsed from argv, so we enforce the name here.
if logging.getLevelName(LOGLEVEL) == 'DEBUG':
DEBUG = True
else:
DEBUG = False
Expand All @@ -264,7 +271,6 @@ def main():


logging.basicConfig(level=LOGLEVEL)

try:
selected_backend = guess_backend(CONFIG['PRINTER']['PRINTER'])
except ValueError:
Expand Down
4 changes: 2 additions & 2 deletions font_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_fonts(folder=None):
else:
cmd = ['fc-list', ':', 'file', 'family', 'style']
for line in subprocess.check_output(cmd).decode('utf-8').split("\n"):
logger.debug(line)
# logger.debug(line)
line.strip()
if not line: continue
if 'otf' not in line and 'ttf' not in line: continue
Expand All @@ -38,5 +38,5 @@ def get_fonts(folder=None):
try: fonts[families[i]]
except: fonts[families[i]] = dict()
fonts[families[i]][styles[i]] = path
logger.debug("Added this font: " + str((families[i], styles[i], path)))
# logger.debug("Added this font: " + str((families[i], styles[i], path)))
return fonts
21 changes: 20 additions & 1 deletion views/labeldesigner.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse4">
<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
Amount
</a>
</h4>
</div>
<div id="collapse4" class="panel-collapse collapse">
<div class="chooser panel-body">
<label for="labelCount">Label Count:</label>
<div class="input-group labelCount">
<input id="labelCount" class="form-control" type="number" min="1" max="200" value="1" required>
</div>
</div> <!-- class="chooser panel-body" -->
</div>
</div>
</fieldset>
</div>
<div class="col-md-4">
Expand Down Expand Up @@ -149,7 +167,8 @@ function formData() {
margin_top: $('#marginTop').val(),
margin_bottom: $('#marginBottom').val(),
margin_left: $('#marginLeft').val(),
margin_right: $('#marginRight').val()
margin_right: $('#marginRight').val(),
label_count: $('#labelCount').val(),
}
}

Expand Down

0 comments on commit 17dbdf1

Please sign in to comment.