Added address printing amongst other things... overkill going to remove
This commit is contained in:
parent
084ab3174b
commit
87832d4d90
12
flake.lock
generated
12
flake.lock
generated
@ -41,11 +41,11 @@
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1720657034,
|
||||
"narHash": "sha256-nPhbeFdyN8yn+EXmnPcBWisoypndtQbNIhSKmAinv3E=",
|
||||
"lastModified": 1721303309,
|
||||
"narHash": "sha256-/+Yw4tW/mcTRKmkEAO64ObzCQClpSUZpk2flUD9GDHE=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "212defe037698e18fc9521dfe451779a8979844c",
|
||||
"rev": "7e2fb8e0eb807e139d42b05bf8e28da122396bed",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@ -80,11 +80,11 @@
|
||||
"treefmt-nix": "treefmt-nix"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1719850884,
|
||||
"narHash": "sha256-UU/lVTHFx0GpEkihoLJrMuM9DcuhZmNe3db45vshSyI=",
|
||||
"lastModified": 1721039874,
|
||||
"narHash": "sha256-XANsG9GYHip8pxZpbqKf/YGv8tIa0xTh289Y+WNBNfw=",
|
||||
"owner": "nix-community",
|
||||
"repo": "poetry2nix",
|
||||
"rev": "42262f382c68afab1113ebd1911d0c93822d756e",
|
||||
"rev": "d11c01e58587e5f21037ed6477465a7f26a32e27",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
2
poetry.lock
generated
2
poetry.lock
generated
@ -195,4 +195,4 @@ files = [
|
||||
[metadata]
|
||||
lock-version = "2.0"
|
||||
python-versions = "^3.10"
|
||||
content-hash = "40c1e26a464774347a91a748b5701b2cb4febebe202102e0dd47fad6d2360434"
|
||||
content-hash = "98cc1c5fe455fa307b03e70ecc55ca624e70d7d4fdc613b60638c9e5dd7b8be1"
|
||||
|
@ -15,6 +15,7 @@ qlprint = "qlprint:qlprint"
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.10"
|
||||
brother-ql = "^0.9.4"
|
||||
pillow = "^10.4.0"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
|
@ -4,42 +4,93 @@ 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!")
|
||||
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("-c", "--copies", type=int, default=1, help="number of copies of the image/images to be printed")
|
||||
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 \\")
|
||||
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
|
||||
|
||||
def qlprint():
|
||||
parser = create_parser()
|
||||
args = parser.parse_args()
|
||||
if "." in args.image:
|
||||
do_print = False
|
||||
if args.image is not None:
|
||||
if "." in args.image:
|
||||
print("Processing input image")
|
||||
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"
|
||||
print("Converting images")
|
||||
if args.crop:
|
||||
task = subprocess.Popen(["magick", "-density", "300", args.image, "-trim" if args.crop else "", "-resize", "696x", filename])
|
||||
task.wait()
|
||||
do_print = True
|
||||
else:
|
||||
print("Processing input recovery key")
|
||||
path = f"/tmp/{args.image}"
|
||||
name = args.image
|
||||
if not os.path.isdir(path):
|
||||
print(f"{path} does not exist. If you are trying to continue printing can you check that the recovery key is correct. If you are trying to print a new image perhaps the extension is missing from filename (.png, .pdf, etc)")
|
||||
elif len(os.listdir(path)) == 0:
|
||||
print(f"{path} exists but is empty - there is a chance that everything has already been printed. If not and if you are trying to continue printing can you check that the recovery key is correct. If you are trying to print a new image perhaps the extension is missing from filename (.png, .pdf, etc)")
|
||||
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"
|
||||
print("Converting images")
|
||||
task = subprocess.Popen(["magick", "-density", "300", args.image, "-trim", "-resize", "696x", filename])
|
||||
task.wait()
|
||||
do_print = True
|
||||
else:
|
||||
path = f"/tmp/{args.image}"
|
||||
name = args.image
|
||||
if not os.path.isdir(path):
|
||||
print(f"{path} does not exist. If you are trying to continue printing can you check that the recovery key is correct. If you are trying to print a new image perhaps the extension is missing from filename (.png, .pdf, etc)")
|
||||
do_print = False
|
||||
elif len(os.listdir(path)) == 0:
|
||||
print(f"{path} exists but is empty - there is a chance that everything has already been printed. If not and if you are trying to continue printing can you check that the recovery key is correct. If you are trying to print a new image perhaps the extension is missing from filename (.png, .pdf, etc)")
|
||||
do_print = False
|
||||
else:
|
||||
print(f"Continuing print from {path}")
|
||||
do_print = True
|
||||
img.save(filename)
|
||||
do_print = True
|
||||
|
||||
if do_print:
|
||||
if len(os.listdir(path)) > 1:
|
||||
files = sorted(glob.glob(f"{path}/{name}*.png"), key=lambda arg: int(os.path.splitext(os.path.basename(arg))[0].split("-")[1]))
|
||||
|
Loading…
Reference in New Issue
Block a user