diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1a91bf3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${workspaceFolder}/generate_label_example.py", + "console": "integratedTerminal", + "args": [] + } + ] +} \ No newline at end of file diff --git a/generate_label_example.py b/generate_label_example.py new file mode 100644 index 0000000..e13844e --- /dev/null +++ b/generate_label_example.py @@ -0,0 +1,15 @@ +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.print_image(ff, cut=False) + printer.print_image(ff, cut=False) + printer.print_image(ff, cut=True) +finally: + os.remove(ff) diff --git a/shimatta_label/__init__.py b/shimatta_label/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shimatta_label/brother_ql_wrapper.py b/shimatta_label/brother_ql_wrapper.py new file mode 100644 index 0000000..8d7949a --- /dev/null +++ b/shimatta_label/brother_ql_wrapper.py @@ -0,0 +1,22 @@ +import subprocess, os + +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) + + res = subprocess.run(cmdline, env=env_vars) + return res.returncode \ No newline at end of file diff --git a/shimatta_label/fonts/OpenSans-Bold.ttf b/shimatta_label/fonts/OpenSans-Bold.ttf new file mode 100644 index 0000000..7b52945 Binary files /dev/null and b/shimatta_label/fonts/OpenSans-Bold.ttf differ diff --git a/shimatta_label/fonts/OpenSans-Regular.ttf b/shimatta_label/fonts/OpenSans-Regular.ttf new file mode 100644 index 0000000..2e31d02 Binary files /dev/null and b/shimatta_label/fonts/OpenSans-Regular.ttf differ diff --git a/shimatta_label/label_image.py b/shimatta_label/label_image.py new file mode 100644 index 0000000..6e8df90 --- /dev/null +++ b/shimatta_label/label_image.py @@ -0,0 +1,89 @@ +import sys +import os +from PIL import Image, ImageDraw, ImageFont +import tempfile + +class Label(): + pixels_x = 155 + pixels_y = 106 + bg_color = (255, 255, 255) + + def __init__(self, skip_create = False): + me_path = os.path.dirname(__file__) + self.bold_font_fname = os.path.join(me_path, 'OpenSans-Bold.ttf') + self.regular_font_fname = os.path.join(me_path, 'OpenSans-Regular.ttf') + + if not skip_create: + self.create() + + 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): + d = ImageDraw.Draw(self.img) + if font_file is None or font_file == 'regular': + font_file = self.regular_font_fname + elif font_file == 'bold': + font_file = self.bold_font_fname + + font_fits = False + orig_size = size + orig_pos_x = pos_x + orig_pos_y = pos_y + while not font_fits: + fnt = ImageFont.truetype(font_file, size) + w, h = d.textsize(text, font=fnt) + + if centered: + pos_x = orig_pos_x - w / 2 + pos_y = orig_pos_y - h / 2 + else: + pos_x = orig_pos_x + pos_y = orig_pos_y + + if not scale_to_fit: + font_fits = True + break + + if pos_x >= 0 and pos_x + w <= self.pixels_x: + font_fits = True + else: + size = size - 1 + if size != orig_size: + print('Rescaled font to size:', size) + + d.text((pos_x, pos_y), text, font=fnt, fill=color) + + def save(self, fname = None): + """ + Save Label to file. If no fname is supplied, a tempfile is created and its filename is returend + """ + if self.img is None: + raise Exception('Must create image first') + + if fname is None or fname == '': + fobj = tempfile.mkstemp(suffix='.png', prefix='label_', text=False) + with os.fdopen(fobj[0], mode="w+b") as file: + self.img.save(file, format='PNG') + fname = fobj[1] + else: + self.img.save(fname) + + return fname + + +class MiceToiletLabel(Label): + + pixels_x = 155 + pixels_y = 106 + + def __init__(self): + super().__init__(skip_create=True) + 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) + if line1: + self.draw_text(line1, self.pixels_x/2, 55, size=20, centered=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