From e2b7e2d7f6c7d5c6ba1dec3dd48902fd60e39f86 Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Sun, 17 Sep 2017 21:28:17 +0200 Subject: [PATCH] create_label(): first .paste(), then .convert("L") The advantage is that the conversion to grayscale is now done in the same way for all labels just before the conversion to b/w. The memory footprint shouln't increase too much. And profiling showed that the the performance for small labels that need pasting didn't deteriorate. --- brother_ql/brother_ql_create.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/brother_ql/brother_ql_create.py b/brother_ql/brother_ql_create.py index e62e700..7fa90fd 100755 --- a/brother_ql/brother_ql_create.py +++ b/brother_ql/brother_ql_create.py @@ -84,13 +84,11 @@ def create_label(qlr, image, label_size, threshold=70, cut=True, dither=False, * hsize = int((dots_printable[0] / im.size[0]) * im.size[1]) im = im.resize((dots_printable[0], hsize), Image.ANTIALIAS) logger.warning('Need to resize the image...') - im = im.convert("L") if im.size[0] < device_pixel_width: - new_im = Image.new("L", (device_pixel_width, im.size[1]), 255) + new_im = Image.new(im.mode, (device_pixel_width, im.size[1]), (255,)*len(im.mode)) new_im.paste(im, (device_pixel_width-im.size[0]-right_margin_dots, 0)) im = new_im elif label_specs['kind'] in (DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL): - im = im.convert("L") if rotate == 'auto': if im.size[0] == dots_printable[1] and im.size[1] == dots_printable[0]: im = im.rotate(90, expand=True) @@ -98,10 +96,11 @@ def create_label(qlr, image, label_size, threshold=70, cut=True, dither=False, * im = im.rotate(rotate, expand=True) if im.size[0] != dots_printable[0] or im.size[1] != dots_printable[1]: sys.exit("Check your image dimensions. Expecting: " + str(dots_printable)) - new_im = Image.new("L", (device_pixel_width, dots_printable[1]), 255) + new_im = Image.new(im.mode, (device_pixel_width, dots_printable[1]), (255,)*len(im.mode)) new_im.paste(im, (device_pixel_width-im.size[0]-right_margin_dots, 0)) im = new_im + im = im.convert("L") im = PIL.ImageOps.invert(im) if dither: