Label creation and print
This commit is contained in:
@@ -6,8 +6,11 @@ from io import BytesIO
|
||||
import datetime
|
||||
import textwrap
|
||||
from math import floor
|
||||
|
||||
import PIL.Image
|
||||
import barcode as python_barcode
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import labelprinter
|
||||
|
||||
|
||||
def now(fmt="%Y-%m-%d %H:%M"):
|
||||
@@ -43,7 +46,8 @@ def wrap(text, col_width):
|
||||
return "\n".join(textwrap.wrap(text, col_width))
|
||||
|
||||
|
||||
def container_label(data, barcode_class='code128', fmt='png', logo_file='./static/images/djlogo256.png',owner_name='Dirk Jahnke', description='', **writer_options):
|
||||
def container_label(data, barcode_class='code128', fmt='png', logo_file='./static/images/djlogo256.png',
|
||||
owner_name='Dirk Jahnke', description='', description_line2='', **options):
|
||||
"""Return a barcode's image data.
|
||||
Powered by the Python library ``python-barcode``. See this library's
|
||||
documentation for more details.
|
||||
@@ -60,6 +64,10 @@ def container_label(data, barcode_class='code128', fmt='png', logo_file='./stati
|
||||
svg -> embeddable code for html img tag
|
||||
logo_file
|
||||
File path to logo file to be included
|
||||
owner_name
|
||||
Name of owner printed underneath the logo
|
||||
description
|
||||
Text to be printed on bottom of the label
|
||||
writer_options
|
||||
Various options for the writer to tune the appearance of the barcode
|
||||
(see python-barcode documentation).
|
||||
@@ -86,8 +94,10 @@ def container_label(data, barcode_class='code128', fmt='png', logo_file='./stati
|
||||
"""
|
||||
|
||||
with Image.open(logo_file) as logo_img:
|
||||
font_size = 16
|
||||
logo_img = logo_img.resize((128,128))
|
||||
font_size = 22
|
||||
font_size_small = 16
|
||||
owner_name_font_size = 26
|
||||
logo_img = logo_img.resize((128, 128))
|
||||
logo_img = logo_img.convert('L')
|
||||
threshold = 20
|
||||
logo_img = logo_img.point(lambda p: 255 if p < threshold else 0)
|
||||
@@ -96,26 +106,47 @@ def container_label(data, barcode_class='code128', fmt='png', logo_file='./stati
|
||||
data = str(data).zfill(constructor.digits)
|
||||
writer = {
|
||||
'svg': python_barcode.writer.ImageWriter,
|
||||
'png': python_barcode.writer.ImageWriter
|
||||
'png': python_barcode.writer.ImageWriter,
|
||||
'image': python_barcode.writer.ImageWriter
|
||||
}[fmt]
|
||||
barcode_img = constructor(data, writer=writer())
|
||||
img = barcode_img.render(writer_options=writer_options)
|
||||
img = barcode_img.render(writer_options=options['writer_options'])
|
||||
barcode_xsize, barcode_ysize = img.size
|
||||
logo_xsize, logo_ysize = logo_img.size
|
||||
result_img = Image.new('1', (696, barcode_ysize+20), 1)
|
||||
result_img.paste(logo_img, (0, floor(barcode_ysize/3)-floor(logo_ysize/2)))
|
||||
label_xsize = 696
|
||||
label_ysize = barcode_ysize + font_size if description_line2 == '' else barcode_ysize + 2 * font_size
|
||||
result_img = Image.new('1', (label_xsize, label_ysize), 1)
|
||||
result_img.paste(logo_img, (owner_name_font_size + 10, floor(barcode_ysize / 2) - 20 - floor(logo_ysize / 2)))
|
||||
start_x_for_barcode = logo_xsize + 10
|
||||
max_x_for_barcode = 696 - start_x_for_barcode
|
||||
result_img.paste(img, (start_x_for_barcode + floor((max_x_for_barcode-barcode_xsize)/2), 5))
|
||||
# fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
|
||||
result_img.paste(img, (start_x_for_barcode + floor((max_x_for_barcode - barcode_xsize) / 2), 5))
|
||||
fnt = ImageFont.truetype("./static/fonts/DejaVuSerif.ttf", font_size)
|
||||
|
||||
d = ImageDraw.Draw(result_img)
|
||||
d.text((20,logo_ysize+40), owner_name, font=fnt)
|
||||
d.text((30+floor((660-fnt.getlength(description))/2),barcode_ysize), description, font=fnt)
|
||||
owner_name_fnt = ImageFont.truetype("./static/fonts/DejaVuSerif.ttf", owner_name_font_size)
|
||||
owner_text_img = Image.new('1', (floor(owner_name_fnt.getlength(owner_name)+1), owner_name_font_size+5), 1)
|
||||
d = ImageDraw.Draw(owner_text_img)
|
||||
d.text((0,0), owner_name, font=owner_name_fnt)
|
||||
owner_text_img = owner_text_img.transpose(PIL.Image.ROTATE_90)
|
||||
result_img.paste(owner_text_img, (0, 10))
|
||||
|
||||
if fmt == 'png':
|
||||
d = ImageDraw.Draw(result_img)
|
||||
#d.text((20, logo_ysize), owner_name, font=fnt)
|
||||
if fnt.getlength(description) > 660:
|
||||
fnt_small = ImageFont.truetype("./static/fonts/DejaVuSerif.ttf", font_size_small)
|
||||
d.text((0, barcode_ysize), description, font=fnt_small)
|
||||
if description_line2 != '':
|
||||
d.text((0, barcode_ysize + font_size), description_line2, font=fnt_small)
|
||||
else:
|
||||
#d.text((30 + floor((660 - fnt.getlength(description)) / 2), barcode_ysize), description, font=fnt)
|
||||
d.text((0, barcode_ysize), description, font=fnt)
|
||||
if description_line2 != '':
|
||||
d.text((0, barcode_ysize + font_size), description_line2, font=fnt)
|
||||
|
||||
if fmt == 'image':
|
||||
return result_img
|
||||
elif fmt == 'png':
|
||||
return pil_to_html_imgdata(result_img, fmt='PNG')
|
||||
else:
|
||||
prefix = "data:image/svg+xml;charset=utf-8;base64,"
|
||||
return prefix + base64.b64encode(result_img).decode()
|
||||
|
||||
|
Reference in New Issue
Block a user