From bd0db220e34832e9a1a9841db710912d37716a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 28 Dec 2021 18:13:36 +0100 Subject: [PATCH] Add storage labels and Fix #1 --- generate_label_example.py | 15 ---------- generate_storage_label.py | 9 ++++++ print_labels_from_csv.py | 6 ++-- shimatta_label/brother_ql_wrapper.py | 43 +++++++++++++++++----------- shimatta_label/label_image.py | 18 ++++++++---- shimatta_label/storage_label.py | 23 +++++++++++++++ 6 files changed, 72 insertions(+), 42 deletions(-) delete mode 100644 generate_label_example.py create mode 100644 generate_storage_label.py create mode 100644 shimatta_label/storage_label.py diff --git a/generate_label_example.py b/generate_label_example.py deleted file mode 100644 index 2dfd3a5..0000000 --- a/generate_label_example.py +++ /dev/null @@ -1,15 +0,0 @@ -import shimatta_label.label_image as li -import shimatta_label.brother_ql_wrapper as ql_wrapper -import os - -label = li.MiceToiletLabel() -label.put_text('100n 10%% biggggg', 'X7R 50V', 'AVX') -ff = label.save() -print('Created Tempfile: ', ff) -try: - printer = ql_wrapper.BrotherQlPrinter(printer_connection='usb://0x04f9:0x209b', model='QL-800') - printer.print_image(ff, cut=False) - printer.print_image(ff, cut=False) - printer.print_image(ff, cut=True) -finally: - os.remove(ff) diff --git a/generate_storage_label.py b/generate_storage_label.py new file mode 100644 index 0000000..6110fea --- /dev/null +++ b/generate_storage_label.py @@ -0,0 +1,9 @@ +import shimatta_label.label_image as li +import shimatta_label.brother_ql_wrapper as ql_wrapper +from shimatta_label.storage_label import StorageLabel + +label = StorageLabel() +label.put_content('[stor_uuid]1c5cf30a-829c-4716-abfc-9ae2d4d493b2', '/Test1') + +printer = ql_wrapper.BrotherQlPrinter(label_format='29x90') +printer.print_image(label.get_pillow_image(), rotation=90, cut=True) \ No newline at end of file diff --git a/print_labels_from_csv.py b/print_labels_from_csv.py index 33753a0..aa5b52c 100644 --- a/print_labels_from_csv.py +++ b/print_labels_from_csv.py @@ -13,14 +13,12 @@ else: df = pd.read_csv(example_data_path, converters={i: str for i in range(3)}) -printer = ql_wrapper.BrotherQlPrinter(model='QL-800', printer_connection='usb://0x04f9:0x209b') +printer = ql_wrapper.BrotherQlPrinter(model='QL-800', printer_connection='usb://0x04f9:0x209b', label_format='12') for _, row in df.iterrows(): label = li.MiceToiletLabel() label.put_text(row['Heading'], row['Line1'], row['Line2']) - file_path = label.save() cut = False if row['Cut'] == 1: cut = True - printer.print_image(file_path, cut = cut, rotation=270) - os.remove(file_path) + printer.print_image(label.get_pillow_image(), cut = cut, rotation=270) diff --git a/shimatta_label/brother_ql_wrapper.py b/shimatta_label/brother_ql_wrapper.py index 8d7949a..168492a 100644 --- a/shimatta_label/brother_ql_wrapper.py +++ b/shimatta_label/brother_ql_wrapper.py @@ -1,22 +1,31 @@ import subprocess, os +from brother_ql.conversion import convert +from brother_ql.backends.helpers import send +from brother_ql.raster import BrotherQLRaster class BrotherQlPrinter(): - def __init__(self, model='QL-800', printer_connection='usb://0x04f9:0x209b'): + + backend = 'pyusb' + + def __init__(self, model='QL-800', printer_connection='usb://0x04f9:0x209b', label_format='12', red_label=False): self.model = model self.printer = printer_connection - - def print_image(self, image, label='12', rotation=90, cut=True): - env_vars = os.environ - env_vars['BROTHER_QL_MODEL'] = self.model - env_vars['BROTHER_QL_PRINTER'] = self.printer - cutstr = '' - - - cmdline = ['brother_ql', 'print', '-l', f'{label}', '-r', f'{rotation}'] - if not cut: - cmdline.append('--no-cut') - cmdline.append(f'{image}') - print(cmdline) - - res = subprocess.run(cmdline, env=env_vars) - return res.returncode \ No newline at end of file + self.label = label_format + self.red_label = red_label + + def print_image(self, image, rotation='Auto', cut=True): + qlr = BrotherQLRaster(self.model) + qlr.exception_on_warning = True + instructions = convert(qlr=qlr, + images = [image], + label=self.label, + rotate = str(rotation), + threshold=70.0, + dither = False, + compress = False, + red = self.red_label, + dpi_600=False, + hq = True, + cut = cut + ) + send(instructions=instructions, printer_identifier=self.printer, backend_identifier=self.backend, blocking=True) \ No newline at end of file diff --git a/shimatta_label/label_image.py b/shimatta_label/label_image.py index 6e8df90..4b0bf95 100644 --- a/shimatta_label/label_image.py +++ b/shimatta_label/label_image.py @@ -19,7 +19,7 @@ class Label(): def create(self): self.img = Image.new('RGB', (self.pixels_x, self.pixels_y), color=self.bg_color) - def draw_text(self, text : str, pos_x, pos_y, color=(0,0,0), size=10, font_file=None, centered=False, scale_to_fit=False): + def draw_text(self, text : str, pos_x, pos_y, color=(0,0,0), size=10, font_file=None, centered_x=False, centered_y=False, scale_to_fit=False): d = ImageDraw.Draw(self.img) if font_file is None or font_file == 'regular': font_file = self.regular_font_fname @@ -34,11 +34,14 @@ class Label(): fnt = ImageFont.truetype(font_file, size) w, h = d.textsize(text, font=fnt) - if centered: + if centered_x: pos_x = orig_pos_x - w / 2 - pos_y = orig_pos_y - h / 2 else: pos_x = orig_pos_x + + if centered_y: + pos_y = orig_pos_y - h / 2 + else: pos_y = orig_pos_y if not scale_to_fit: @@ -70,6 +73,9 @@ class Label(): self.img.save(fname) return fname + + def get_pillow_image(self): + return self.img class MiceToiletLabel(Label): @@ -82,8 +88,8 @@ class MiceToiletLabel(Label): self.create() def put_text(self, heading, line1=None, line2=None): - self.draw_text(heading, self.pixels_x/2, 20, size=25, font_file='bold', centered=True, scale_to_fit=True) + self.draw_text(heading, self.pixels_x/2, 20, size=25, font_file='bold', centered_x=True, centered_y=True, scale_to_fit=True) if line1: - self.draw_text(line1, self.pixels_x/2, 55, size=20, centered=True, scale_to_fit=True) + self.draw_text(line1, self.pixels_x/2, 55, size=20, centered_x=True, centered_y=True, scale_to_fit=True) if line2: - self.draw_text(line2, self.pixels_x/2, 85, size=20, centered=True, scale_to_fit=True) \ No newline at end of file + self.draw_text(line2, self.pixels_x/2, 85, size=20, centered_x=True, centered_y=True, scale_to_fit=True) diff --git a/shimatta_label/storage_label.py b/shimatta_label/storage_label.py new file mode 100644 index 0000000..23244a9 --- /dev/null +++ b/shimatta_label/storage_label.py @@ -0,0 +1,23 @@ +from .label_image import Label +import qrcode + +class StorageLabel(Label): + pixels_x = 991 + pixels_y = 306 + + def __init__(self): + super().__init__() + + def put_content(self, qr_data, storage_name): + self.draw_text(storage_name, 320, self.pixels_y/2, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True) + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=8, + border=0, + ) + qr.add_data(qr_data) + qr.make(fit=True) + qr_image = qr.make_image(fill_color="black", back_color="white") + qr_y_size = qr_image.size[1] + self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2))) \ No newline at end of file