1
0
Fork 0
etikettenpost/post.py

127 lines
4.8 KiB
Python
Executable File

#!/usr/bin/python3
##################
# EtikettenPost: erstellt aus verschiedenen Quelldateien PDFs,
# die mit Hilfe eines Brother Etikettendruckers auf 62mm Rollenpapier
# ausgedruckt werden können, um damit Briefe und Pakete zu frankieren.
#
# (c) 2019 Anna Christina Naß <christina@imzadi.de>
# Lizensiert unter GPL 3, siehe Datei: LICENSE
##################
import wand.image
import numpy
import PIL
import io
from argparse import ArgumentParser, FileType
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm, mm
#from reportlab.platypus import SimpleDocTemplate, Image
# Hilfsfunktionen:
def getPIL(bild):
# wandelt ein wand.image.Image in ein PIL.Image um
# damit kann ein PDF-Schnipsel direkt in einen ReportLab-Canvas eingefügt werden
img_buffer = numpy.asarray(bytearray(bild.make_blob(format='png')), dtype='uint8')
return PIL.Image.open(io.BytesIO(img_buffer))
def details(bild):
# gibt zu einem wand.image.Image die Größe und das Verhältnis aus
return "Größe: " + str(bild.size) + " Ratio: " + str(bild.width/bild.height)
def getH(bild, w):
# um das Seitenverhältnis beizubehalten:
# gibt zu gegebener Breite die passende Höhe zurück
return w / (bild.width/bild.height)
def getW(bild, h):
# um das Seitenverhältnis beizubehalten:
# gibt zu gegebener Höhe die passende Breite zurück
return h / (bild.height/bild.width)
def makeDHLPaketmarke(eingabe, ausgabe):
# Erstellt ein Paketetikett von einer DIN A4-DHL-Paketmarke
# Standard DHL-Paketmarke:
# Bild 90° nach rechts drehen, dann folgende Koordinaten:
# - 2045x108, 2633x204: obere Zeile "Paket ..."
# - 2873x121, 3205x173: DHL Logo
# - 2045x220, 2894x871: Absender+Empfänger
# - 2045x887, 2771x1047: GO GREEN/Sendungsnr.
# - 2306x1533, 2995x2358: Barcodes
print("DHL-Paketmarke")
with wand.image.Image(filename=eingabe, resolution=300) as inPdf:
inPdf.rotate(90)
ObereZeile = inPdf[2045:2633, 108:204]
DHLlogo = inPdf[2873:3205, 121:173]
AbsEmpf = inPdf[2045:2894, 220:871]
Sendungsnr = inPdf[2045:2771, 887:1047]
Sendungsnr.rotate(270)
Barcodes = inPdf[2306:2995, 1533:2358]
outPdf = canvas.Canvas(ausgabe, pagesize=(160*mm, 62*mm))
outPdf.drawInlineImage(getPIL(ObereZeile), 2*mm, 56*mm,
width = getW(ObereZeile,5)*mm, height=5*mm)
outPdf.drawInlineImage(getPIL(DHLlogo), 45*mm, 56*mm,
width = getW(DHLlogo,5)*mm, height=5*mm)
outPdf.drawInlineImage(getPIL(AbsEmpf), 3*mm, 1*mm,
width = getW(AbsEmpf,54)*mm, height=54*mm)
outPdf.drawInlineImage(getPIL(Sendungsnr), 80*mm, 1*mm,
width = getW(Sendungsnr,60)*mm, height=60*mm)
outPdf.drawInlineImage(getPIL(Barcodes), 100*mm, 1*mm,
width = getW(Barcodes,60)*mm, height=60*mm)
outPdf.save()
def makeBriefmarke(eingabe, ausgabe):
# Erstellt eine Briefmarke aus einer ePost-PDF-Datei
with wand.image.Image(filename=eingabe, resolution=300) as inPdf:
porto = inPdf[146:580, 32:266]
porto.rotate(270)
outPdf = canvas.Canvas(ausgabe, pagesize=(37*mm, 62*mm))
outPdf.drawInlineImage(getPIL(porto), 2*mm, 2*mm,
width = getW(porto,58)*mm, height=58*mm)
outPdf.save()
def makeAmazonRetourGIF(eingabe, ausgabe):
# Erstellt ein Paketetikett aus einer Amazon-Retouren-GIF-Datei
with wand.image.Image(filename=eingabe) as inGif:
inGif.rotate(90)
absender = getPIL(inGif[144:555, 70:258])
empfaenger = getPIL(inGif[72:555, 730:1032])
code1 = getPIL(inGif[325:810, 362:686])
code2 = getPIL(inGif[195:1005, 1100:1725])
outPdf = canvas.Canvas(ausgabe, pagesize=(130*mm, 62*mm))
outPdf.drawInlineImage(absender, 2*mm, 45*mm, width = getW(absender,15)*mm, height=15*mm)
outPdf.drawInlineImage(empfaenger, 2*mm, 0*mm, width = getW(empfaenger,39)*mm, height=39*mm)
outPdf.drawInlineImage(code1, 88.5*mm, 39*mm, width = getW(code1,22)*mm, height=22*mm)
outPdf.drawInlineImage(code2, 80*mm, 0*mm, width = getW(code2,37)*mm, height=37*mm)
outPdf.save()
### main():
parser = ArgumentParser()
parser.add_argument("inputfile", help="Eingabedatei")
parser.add_argument("-o", dest="output", metavar='OUTPUT', help="Ausgabedatei (PDF)")
parser.add_argument("-t", dest="type", choices=["DHL", "ePost", "AmazonGIF"], default="DHL", help="Art der Eingabedatei (Standard: DHL)")
args = parser.parse_args()
if args.output:
outfile = args.output
else:
outfile = args.inputfile + "-out.pdf"
infile = args.inputfile
if args.type=="DHL":
makeDHLPaketmarke(infile, outfile)
elif args.type=="ePost":
makeBriefmarke(infile, outfile)
elif args.type=="AmazonGIF":
makeAmazonRetourGIF(infile, outfile)