commit 493d600a4791c4fe99e58b493447b980d9de95c0 Author: Pedro Sá Couto Date: Sat Oct 12 15:02:45 2019 +0200 done diff --git a/addwatermarktopdf.sh b/addwatermarktopdf.sh new file mode 100755 index 0000000..ae38cdd --- /dev/null +++ b/addwatermarktopdf.sh @@ -0,0 +1,3 @@ +python3 burstpdf.py +python3 stick.py +./mergepdf.sh diff --git a/burstpdf.py b/burstpdf.py new file mode 100644 index 0000000..bab1715 --- /dev/null +++ b/burstpdf.py @@ -0,0 +1,43 @@ +#Based in the code in https://iq.opengenus.org/pdf_to_image_in_python/ + +import pdf2image +from PIL import Image +import time + +#DECLARE CONSTANTS +PDF_PATH = "target.pdf" +DPI = 200 +FIRST_PAGE = None +LAST_PAGE = None +FORMAT = 'png' +THREAD_COUNT = 1 +USERPWD = None +USE_CROPBOX = False +STRICT = False + +def pdftopil(): + #This method reads a pdf and converts it into a sequence of images + #PDF_PATH sets the path to the PDF file + #dpi parameter assists in adjusting the resolution of the image + #first_page parameter allows you to set a first page to be processed by pdftoppm + #last_page parameter allows you to set a last page to be processed by pdftoppm + #fmt parameter allows to set the format of pdftoppm conversion (PpmImageFile, TIFF) + #thread_count parameter allows you to set how many thread will be used for conversion. + #userpw parameter allows you to set a password to unlock the converted PDF + #use_cropbox parameter allows you to use the crop box instead of the media box when converting + #strict parameter allows you to catch pdftoppm syntax error with a custom type PDFSyntaxError + + start_time = time.time() + pil_images = pdf2image.convert_from_path(PDF_PATH, dpi=DPI, first_page=FIRST_PAGE, last_page=LAST_PAGE, fmt=FORMAT, thread_count=THREAD_COUNT, userpw=USERPWD, use_cropbox=USE_CROPBOX, strict=STRICT) + print ("Time taken : " + str(time.time() - start_time)) + return pil_images + +def save_images(pil_images): + d = 1 + for image in pil_images: + image.save(("split/page%d"%d) + ".png") + d += 1 + +if __name__ == "__main__": + pil_images = pdftopil() + save_images(pil_images) diff --git a/mergepdf.sh b/mergepdf.sh new file mode 100755 index 0000000..06ccf45 --- /dev/null +++ b/mergepdf.sh @@ -0,0 +1 @@ +convert "split/*.{png,jpeg}" -quality 100 outfile.pdf diff --git a/stick.py b/stick.py new file mode 100644 index 0000000..3420755 --- /dev/null +++ b/stick.py @@ -0,0 +1,27 @@ +from PIL import Image +import PIL.ImageOps + +#open both the image and watermark +image = Image.open('split/page1.png') +logo = Image.open('merged.png') + +#rotate the watermark +rotatedlogo = logo.rotate(18, expand=True) +#invert the watermark +invertedlogo = PIL.ImageOps.invert(rotatedlogo) +invertedlogo.save('rotatedlogo.png') + +#rescaling the logo +basewidth = 1050 +finallogo = Image.open("rotatedlogo.png") +wpercent = (basewidth/float(finallogo.size[0])) +hsize = int((float(finallogo.size[1])*float(wpercent))) +finallogo = finallogo.resize((basewidth,hsize), Image.ANTIALIAS) + +#overlaying +image_copy = image.copy() +#if you want to position in the bottom right corner use: +#position = ((image_copy.width - logo.width), (image_copy.height - logo.height)) +position = ((100), (750)) +image_copy.paste(finallogo, position) +image_copy.save('split/page1.png')