Add storage labels and Fix #1

This commit is contained in:
Mario Hüttel 2021-12-28 18:13:36 +01:00
parent 796f1da1e4
commit bd0db220e3
6 changed files with 72 additions and 42 deletions

View File

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

View File

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

View File

@ -13,14 +13,12 @@ else:
df = pd.read_csv(example_data_path, converters={i: str for i in range(3)}) 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(): for _, row in df.iterrows():
label = li.MiceToiletLabel() label = li.MiceToiletLabel()
label.put_text(row['Heading'], row['Line1'], row['Line2']) label.put_text(row['Heading'], row['Line1'], row['Line2'])
file_path = label.save()
cut = False cut = False
if row['Cut'] == 1: if row['Cut'] == 1:
cut = True cut = True
printer.print_image(file_path, cut = cut, rotation=270) printer.print_image(label.get_pillow_image(), cut = cut, rotation=270)
os.remove(file_path)

View File

@ -1,22 +1,31 @@
import subprocess, os import subprocess, os
from brother_ql.conversion import convert
from brother_ql.backends.helpers import send
from brother_ql.raster import BrotherQLRaster
class BrotherQlPrinter(): 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.model = model
self.printer = printer_connection self.printer = printer_connection
self.label = label_format
def print_image(self, image, label='12', rotation=90, cut=True): self.red_label = red_label
env_vars = os.environ
env_vars['BROTHER_QL_MODEL'] = self.model def print_image(self, image, rotation='Auto', cut=True):
env_vars['BROTHER_QL_PRINTER'] = self.printer qlr = BrotherQLRaster(self.model)
cutstr = '' qlr.exception_on_warning = True
instructions = convert(qlr=qlr,
images = [image],
cmdline = ['brother_ql', 'print', '-l', f'{label}', '-r', f'{rotation}'] label=self.label,
if not cut: rotate = str(rotation),
cmdline.append('--no-cut') threshold=70.0,
cmdline.append(f'{image}') dither = False,
print(cmdline) compress = False,
red = self.red_label,
res = subprocess.run(cmdline, env=env_vars) dpi_600=False,
return res.returncode hq = True,
cut = cut
)
send(instructions=instructions, printer_identifier=self.printer, backend_identifier=self.backend, blocking=True)

View File

@ -19,7 +19,7 @@ class Label():
def create(self): def create(self):
self.img = Image.new('RGB', (self.pixels_x, self.pixels_y), color=self.bg_color) 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) d = ImageDraw.Draw(self.img)
if font_file is None or font_file == 'regular': if font_file is None or font_file == 'regular':
font_file = self.regular_font_fname font_file = self.regular_font_fname
@ -34,11 +34,14 @@ class Label():
fnt = ImageFont.truetype(font_file, size) fnt = ImageFont.truetype(font_file, size)
w, h = d.textsize(text, font=fnt) w, h = d.textsize(text, font=fnt)
if centered: if centered_x:
pos_x = orig_pos_x - w / 2 pos_x = orig_pos_x - w / 2
pos_y = orig_pos_y - h / 2
else: else:
pos_x = orig_pos_x pos_x = orig_pos_x
if centered_y:
pos_y = orig_pos_y - h / 2
else:
pos_y = orig_pos_y pos_y = orig_pos_y
if not scale_to_fit: if not scale_to_fit:
@ -70,6 +73,9 @@ class Label():
self.img.save(fname) self.img.save(fname)
return fname return fname
def get_pillow_image(self):
return self.img
class MiceToiletLabel(Label): class MiceToiletLabel(Label):
@ -82,8 +88,8 @@ class MiceToiletLabel(Label):
self.create() self.create()
def put_text(self, heading, line1=None, line2=None): 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: 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: 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)

View File

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