Sfoglia il codice sorgente

Add storage labels and Fix #1

Mario Hüttel 2 anni fa
parent
commit
bd0db220e3

+ 0 - 15
generate_label_example.py

@@ -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)

+ 9 - 0
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)

+ 2 - 4
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)

+ 26 - 17
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'):
-        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)
+    backend = 'pyusb'
 
-        res = subprocess.run(cmdline, env=env_vars)
-        return res.returncode
+    def __init__(self, model='QL-800', printer_connection='usb://0x04f9:0x209b', label_format='12', red_label=False):
+        self.model = model
+        self.printer = printer_connection
+        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)

+ 12 - 6
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)
+            self.draw_text(line2, self.pixels_x/2, 85, size=20, centered_x=True, centered_y=True, scale_to_fit=True)

+ 23 - 0
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)))