152 lines
4.2 KiB
Python
152 lines
4.2 KiB
Python
from pathlib import Path
|
|
import os
|
|
import time
|
|
import logging
|
|
from PIL import Image
|
|
from watchdog.observers import Observer
|
|
from watchdog.events import LoggingEventHandler
|
|
|
|
from pdf2image import convert_from_path
|
|
|
|
downloads_path = str(Path.home() / "Downloads")
|
|
labelPathDir = downloads_path
|
|
|
|
labelPath = labelPathDir + "/label.pdf"
|
|
labelPathRot90 = labelPathDir + "/label_rot90.pdf"
|
|
|
|
versandLabelSHINNEX = labelPathDir + "/Versandlabel.png"
|
|
productLabelSHINNEX = labelPathDir + "/product-bag-label.png"
|
|
filamentLabelSHINNEX = labelPathDir + "/filament-roll-label.png"
|
|
|
|
|
|
printer_identifier = "tcp://192.168.1.70"
|
|
dpi = 300
|
|
|
|
imageHeight29mm = 343
|
|
imageHeight62mm = 745
|
|
imageHeightVersand102 = 3505
|
|
imageHeightVersandSHX = 1544
|
|
|
|
imageWidth102mm = 1020
|
|
|
|
|
|
def checkTolerance(isNum, shouldNum):
|
|
tol = 10
|
|
maxNum = shouldNum + tol
|
|
minNum = shouldNum - tol
|
|
return (isNum < maxNum and isNum > minNum)
|
|
|
|
|
|
def printPDF(path):
|
|
if (os.path.isfile(path)):
|
|
print("label detected", path)
|
|
pages = convert_from_path(path, dpi)
|
|
for page in pages:
|
|
|
|
try:
|
|
page.save('tmp/label.png', 'PNG')
|
|
|
|
pngHeight = page.height
|
|
if (path == labelPathRot90):
|
|
pngHeight = page.width
|
|
print("height: ", pngHeight)
|
|
|
|
labelHeight = 0
|
|
|
|
if (checkTolerance(pngHeight, imageHeight29mm)):
|
|
labelHeight = 29
|
|
if (checkTolerance(pngHeight, imageHeight62mm)):
|
|
labelHeight = 62
|
|
|
|
if (checkTolerance(pngHeight, imageHeightVersand102)):
|
|
labelHeight = 102
|
|
h = page.height/2
|
|
spacing = 100
|
|
|
|
# box=(left, upper, right, lower)
|
|
page.crop((0, spacing, page.width, h-spacing)
|
|
).save('tmp/label.png', 'PNG')
|
|
|
|
if (labelHeight != 0):
|
|
os.system('brother_ql -m QL-1060N -p ' +
|
|
printer_identifier + ' print -d -l ' + str(labelHeight) + ' tmp/label.png -r 90')
|
|
else:
|
|
print("UNKNOWN LABEL PICTURE HEIGHT!")
|
|
except:
|
|
print("An exception occurred")
|
|
os.remove(path)
|
|
|
|
def printPNG(path):
|
|
if (os.path.isfile(path)):
|
|
print("label detected", path)
|
|
|
|
#try:
|
|
# copy path to tmp folder
|
|
os.system('cp ' + path + ' tmp/label.png')
|
|
|
|
# get image height from png
|
|
img = Image.open(path)
|
|
|
|
pngHeight = img.height
|
|
|
|
labelHeight = 0
|
|
|
|
options = ""
|
|
|
|
if (checkTolerance(pngHeight, imageHeightVersandSHX)):
|
|
labelHeight = 102
|
|
options = "-r 90 "
|
|
|
|
if (checkTolerance(img.width, imageWidth102mm)):
|
|
labelHeight = 102
|
|
|
|
if (labelHeight != 0):
|
|
os.system('brother_ql -m QL-1060N -p ' +
|
|
printer_identifier + ' print -d -c -l ' + str(labelHeight) + ' tmp/label.png ' + options + '--lq')
|
|
else:
|
|
print("UNKNOWN LABEL PICTURE HEIGHT!")
|
|
#except:
|
|
# print("An exception occurred")
|
|
|
|
os.remove(path)
|
|
|
|
|
|
printPDF(labelPath)
|
|
printPDF(labelPathRot90)
|
|
|
|
printPNG(versandLabelSHINNEX)
|
|
printPNG(productLabelSHINNEX)
|
|
printPNG(filamentLabelSHINNEX)
|
|
|
|
|
|
class Event(LoggingEventHandler):
|
|
def on_modified(self, event):
|
|
path = event.src_path
|
|
print(path)
|
|
print(labelPath)
|
|
if (path == labelPath or path == labelPathRot90):
|
|
printPDF(path)
|
|
if (path == versandLabelSHINNEX):
|
|
printPNG(path)
|
|
if (path == productLabelSHINNEX):
|
|
printPNG(path)
|
|
if (path == filamentLabelSHINNEX):
|
|
printPNG(path)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(level=logging.INFO,
|
|
format='%(asctime)s - %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S')
|
|
path = labelPathDir # sys.argv[1] if len(sys.argv) > 1 else '.'
|
|
event_handler = Event()
|
|
observer = Observer()
|
|
observer.schedule(event_handler, path, recursive=True)
|
|
observer.start()
|
|
try:
|
|
while True:
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
observer.stop()
|
|
observer.join()
|