Add first draft of label generation

This commit is contained in:
Mario Hüttel 2021-12-27 20:25:40 +01:00
parent 8a25c4894a
commit ea4d951172
7 changed files with 142 additions and 0 deletions

16
.vscode/launch.json vendored Normal file
View File

@ -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": []
}
]
}

15
generate_label_example.py Normal file
View File

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

View File

View File

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

Binary file not shown.

Binary file not shown.

View File

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