Commit e246e91b authored by Christian Rohlfing's avatar Christian Rohlfing
Browse files

add quality parameter for jpeg compression of watermarked pdfs

parent a794fb15
......@@ -24,6 +24,7 @@ class MainTest(unittest.TestCase):
# Prepare parameter
in_dir = './pdfs'
dpi = 250
quality = 75
pdf_file = '123456_Nachname.pdf'
tmp_dir = os.path.join(self.test_dir, 'tmp')
......@@ -33,7 +34,8 @@ class MainTest(unittest.TestCase):
os.mkdir(out_dir)
# Call function
watermark.watermark_pdf(in_dir, tmp_dir, out_dir, dpi, pdf_file)
watermark.watermark_pdf(in_dir, tmp_dir, out_dir,
dpi, quality, pdf_file)
self.assertTrue(os.listdir(out_dir)[0], '123456_Nachname_w.pdf')
......
......@@ -126,7 +126,8 @@ def remove_transparency(im, bg_colour=(255, 255, 255)):
else:
return im
def watermark_img(img_file, template, dpi):
def watermark_img(img_file, template, dpi, quality):
"""Watermarks image with watermark template
Args:
......@@ -152,7 +153,7 @@ def watermark_img(img_file, template, dpi):
# Save image as PDF and delete original image file
pdf_file = os.path.splitext(img_file)[0] + '.pdf'
out.save(pdf_file, resolution=dpi)
out.save(pdf_file, resolution=dpi, quality=quality)
if os.path.isfile(img_file):
os.remove(img_file)
......@@ -188,7 +189,7 @@ def combine_all_pdfs(pdf_pages, out_dir):
return pdf_file
def watermark_pdf(input_dir, tmp_dir, output_dir, dpi, pdf_file):
def watermark_pdf(input_dir, tmp_dir, output_dir, 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, pdf_file):
template = create_watermark_template(img_files[0], matnum, dpi)
pdf_files = []
for img_file in img_files:
pdf_file = watermark_img(img_file, template, dpi)
pdf_file = watermark_img(img_file, template, dpi, quality)
pdf_files.append(pdf_file)
# Combining watermarked PDF pages into one PDF
......@@ -255,6 +256,8 @@ def main(args):
help="tmp folder. Default: ./tmp/")
parser.add_argument("-d", "--dpi", default="250",
help="dpi parameter for conversion from pdf to images. Default: 250")
parser.add_argument("-q", "--quality", default="75",
help="quality parameter for jpeg. Default: 75")
args = parser.parse_args(args)
infolder = args.infolder
......@@ -262,6 +265,7 @@ def main(args):
cores = int(args.cores)
tmpdir = args.tmp
dpi = int(args.dpi)
quality = int(args.quality)
# Print status
starttime = time.time()
......@@ -280,7 +284,7 @@ Parallel execution with {:d} cores from now on.
if cores > 1:
pool = Pool(cores)
watermark_fun = partial(
watermark_pdf, infolder, tmpdir, outfolder, dpi)
watermark_pdf, infolder, tmpdir, outfolder, dpi, quality)
pdf_files_w = pool.map(watermark_fun, pdf_files)
pool.close()
pool.join()
......@@ -288,7 +292,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, pdf_file)
infolder, tmpdir, outfolder, 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