Commit a7c34f4a authored by Amrita Deb's avatar Amrita Deb
Browse files

Merge branch 'rohlfing-add-fontsize' into 'master'

Add watermark fontsize as user parameter

See merge request !31
parents 81ec3cef a13e985e
......@@ -58,7 +58,7 @@ def convert_pdf_to_img(pdf_file, input_dir, tmp_dir, dpi):
return img_files
def create_watermark_template(img_file, matnum, dpi):
def create_watermark_template(img_file, matnum, fontsize, dpi):
"""Creates transparent image with repeated matriculation number
This function creates a transparent image with the student's matriculation
......@@ -93,7 +93,7 @@ def create_watermark_template(img_file, matnum, dpi):
template = Image.new('RGBA', newsize, (255, 255, 255, 0))
# Font
fnt = ImageFont.truetype('./fonts/arial.ttf', round(75 * dpi/250))
fnt = ImageFont.truetype('./fonts/arial.ttf', round(fontsize * dpi/250))
# Drawing context
d = ImageDraw.Draw(template)
......@@ -191,7 +191,8 @@ def combine_all_pdfs(pdf_pages, out_dir):
return pdf_file
def watermark_pdf(input_dir, tmp_dir, output_dir, dpi, quality, pdf_file):
def watermark_pdf(input_dir, tmp_dir, output_dir,
fontsize, dpi, quality, pdf_file):
"""Watermarkes each page of a given PDF file
Args:
......@@ -214,7 +215,7 @@ def watermark_pdf(input_dir, tmp_dir, output_dir, dpi, quality, pdf_file):
# Watermarking PDF page images
# Create template for first page
template = create_watermark_template(img_files[0], matnum, dpi)
template = create_watermark_template(img_files[0], matnum, fontsize, dpi)
pdf_files = []
for img_file in img_files:
pdf_file = watermark_img(img_file, template, dpi, quality)
......@@ -252,6 +253,8 @@ def main(args):
help="Input folder with PDFs. Default: ./pdfs")
parser.add_argument("-o", "--outfolder", default="./pdfs_watermarked",
help="Output folder of the PDFs. Default: ./pdfs_watermarked")
parser.add_argument("-f", "--fontsize", default="75",
help="Font size of watermark text in points. Default: 75")
parser.add_argument("-c", "--cores", default="1",
help="Number of cores for parallel processing. Default: 1")
parser.add_argument("-t", "--tmp", default="./tmp",
......@@ -266,6 +269,7 @@ def main(args):
outfolder = args.outfolder
cores = int(args.cores)
tmpdir = args.tmp
fontsize = int(args.fontsize)
dpi = int(args.dpi)
quality = int(args.quality)
......@@ -286,7 +290,7 @@ Parallel execution with {:d} cores from now on.
if cores > 1:
pool = Pool(cores)
watermark_fun = partial(
watermark_pdf, infolder, tmpdir, outfolder, dpi, quality)
watermark_pdf, infolder, tmpdir, outfolder, fontsize, dpi, quality)
pdf_files_w = pool.map(watermark_fun, pdf_files)
pool.close()
pool.join()
......@@ -294,7 +298,7 @@ Parallel execution with {:d} cores from now on.
pdf_files_w = []
for pdf_file in pdf_files:
pdf_file_w = watermark_pdf(
infolder, tmpdir, outfolder, dpi, quality, pdf_file)
infolder, tmpdir, outfolder, fontsize, dpi, quality, pdf_file)
pdf_files_w.append(pdf_file_w)
# Print status
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment