Optional whitespace trim using -c 1/0

This commit is contained in:
Tom Charnock 2024-07-19 15:00:36 +02:00
parent 87832d4d90
commit 55f92cc8b6
3 changed files with 3 additions and 50 deletions

2
poetry.lock generated
View File

@ -195,4 +195,4 @@ files = [
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "98cc1c5fe455fa307b03e70ecc55ca624e70d7d4fdc613b60638c9e5dd7b8be1"
content-hash = "40c1e26a464774347a91a748b5701b2cb4febebe202102e0dd47fad6d2360434"

View File

@ -15,7 +15,6 @@ qlprint = "qlprint:qlprint"
[tool.poetry.dependencies]
python = "^3.10"
brother-ql = "^0.9.4"
pillow = "^10.4.0"
[build-system]
requires = ["poetry-core"]

View File

@ -4,20 +4,15 @@ import string
import random
import glob
import subprocess
from PIL import Image, ImageFont, ImageDraw
def create_parser():
parser = argparse.ArgumentParser(
prog="qlprint",
description="Printing tools for Brother QL-570 label printer",
epilog="Have fun printing!",
add_help=False)
parser.add_argument('--help', action='help')
parser.add_argument("-i", "--image", type=str, help="path to image (with extension, i.e. .png) or name of temporary folder to continue printing part way through a failed print job (this would be a random string of 10 letters or numbers, e.g. oet845cgx9)")
parser.add_argument("-a", "--address", type=str, help="address between quotation marks with lines separated by \\")
epilog="Have fun printing!")
parser.add_argument("image", type=str, help="path to image (with extension, i.e. .png) or name of temporary folder to continue printing part way through a failed print job (this would be a random string of 10 letters or numbers, e.g. oet845cgx9)")
parser.add_argument("-n", "--copies", type=int, default=1, help="number of copies of the image/images to be printed")
parser.add_argument("-c", "--crop", type=bool, default=True, help="crop the whitespace out of an image")
parser.add_argument("-h", "--height", type=float, default=50., help="resize address font to limit height of print")
parser.add_argument("-t", "--test", type=bool, default=False, help="run program without printing")
return parser
@ -49,47 +44,6 @@ def qlprint():
else:
print(f"Continuing print from {path}")
do_print = True
elif args.address is not None:
print("Constructing address")
task = subprocess.Popen([f"fc-list | grep 'Jost:style=Regular'"], stdout=subprocess.PIPE, shell=True)
task.wait()
stdout = task.stdout.read().decode()
if len(stdout) > 0:
font_path = stdout.split(" ")[0][:-1]
print(f"Font found at {font_path}")
use_font = True
else:
print("Font not found, using default.")
use_font = False
address = args.address.replace("\\", "\n")
size = 1.
big_enough = False
_img = Image.new("1", (0, 0), 1)
_draw = ImageDraw.Draw(_img)
while not big_enough:
size += 1.
if use_font:
font = ImageFont.truetype(font_path, size)
else:
font = ImageFont.load_default(size)
_, _, width, height = _draw.textbbox((0, 0), text=address, font=font)
if width > 62. * 300. / 25.4 - 30 or height > args.height * 300. / 25.4 - 15:
if use_font:
font = ImageFont.truetype(font_path, size - 1.)
else:
font = ImageFont.load_default(size - 1.)
big_enough = True
img = Image.new("1", (int(62. * 300. / 25.4), height+15), 1)
draw = ImageDraw.Draw(img)
draw.multiline_text((15, 0), address, font=font)
name = "".join(random.choices(string.ascii_lowercase + string.digits, k=10))
path = f"/tmp/{name}"
print(f"Creating temporary directory at {path}. To recover failed prints please pass the recovery key {name}")
os.mkdir(path)
filename = f"{path}/{name}.png"
img.save(filename)
do_print = True
if do_print:
if len(os.listdir(path)) > 1: