How to add images to PDF file in Python?


Python is a programming language that has many features and libraries that allow you to get things done quickly and efficiently. In this blog post, we will be looking at one of these features- how to add images to PDF files in Python. We will be using the PyPDF2 library for this purpose.

First, we need to install the PyPDF2 library. The easiest way to do this is by using the pip tool. If you don’t have pip installed, you can follow the instructions here. Once you have pip installed, open up a terminal window and type the following command:

pip install PyPDF2

This will download and install the PyPDF2 library.
Next, we need to create a new Python file and import the PyPDF2 library. To do this, open up a text editor (I use Visual Studio Code) and create a new file called add_image_to_pdf.py . Then, type the following code into the file:

import PyPDF2
Code language: JavaScript (javascript)

This will import the PyPDF2 library into our Python file so that we can use its functions.
Now, we need to open up our PDF file. To do this, add the following code to your Python file:

pdfFileObj = open('mypdf.pdf', 'rb')
Code language: Python (python)

This will open up the PDF file mypdf.pdf in read-binary mode and store it in the pdfFileObj variable. You will need to replace ‘mypdf.pdf’ with the name of your PDF file.

Once our PDF file is open, we can create a new PyPDF2 object by calling the PdfFileReader() function, like this:

pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
Code language: Python (python)

We will store this object in the pdfReader variable so that we can access its functions later on.

Now, we need to create a new blank PDF page where we can add our image. To do this, add the following code to your Python file:

pdfWriter = PyPDF2.PdfFileWriter()

This will create a new blank PDF page that we can write to. We will store this object in the pdfWriter variable so that we can access its functions later on.

Next, we need to get our image ready to be added to the PDF page. To do this, add the following code to your Python script:

image_obj = Image.open('myimage.png')
Code language: JavaScript (javascript)

This will store our image in memory so that we can work with it. You will need to replace ‘myimage’ with the name of your image file (make sure it’s in PNG format). If your image is in another format (like JPG), you can convert it to PNG using an online converter like CloudConvert.

Once your image is ready, we can resize it using these lines of code:

width, height = image_obj.size image_obj.thumbnaild((width/10 , height/10))

This will resize our image so that it’s 10% of its original size (this is optional but recommended).

We want our image to be small so that it doesn’t take up too much space on the PDF page when we add it later on (you can experiment with different values here).

Finally, we’re ready to add our image to the blank PDF page! To do this Add these lines of Code :

pdfWriter.addImage(image_obj,'png',0 , 0)
Code language: JavaScript (javascript)

This adds our Image at position (0, 0) on The Page, You Can Change These Values To Move The Position Of The Image. Once our Image Is Added All That’s Left Is To Save Our New Page With The Image Added By Calling :

outputStream = output("mynewfile"+'.pdf','wb') pdfWriter.write(outputStream) outputSteram.close()
Code language: JavaScript (javascript)

And That’s It ! You’ve Successfully Added An Image Into A Pdf File Using Python !

Python script 1: An Image Into A Pdf File

In This Python script the module that needs to be imported is named fitz, and this name goes back to the previous name of PyMuPDF.

# !/usr/bin/python import fitz input_file = "example.pdf" output_file = "example-with-barcode.pdf" barcode_image = "barcode.png" # define the position (upper-right corner) image_rectangle = fitz.Rect(450,20,550,120) # retrieve the first page of the PDF file_handle = fitz.open(input_file) first_page = file_handle[0] # add the image first_page.insertImage(image_rectangle, fileName=barcode_image) file_handle.save(output_file)
Code language: Python (python)

Python script 2: An Image Into A Pdf File

Source

import os from fpdf import FPDF #from PIL import Image #pdf = FPDF(orientation = 'L') pdf = FPDF() pdf.add_page() #add a page first #i = Image.open('flower.png') #i_name = os.path.basename('flower.png') #w, h = i.size pdf.image('flower.png') pdf.output("image.pdf", "F") dirs = os.listdir('.') pdf1 = FPDF() #pdf1.add_page() for img in dirs: if os.path.isfile(img) and os.path.splitext(img)[1].lower() in ('.jpg', '.jpeg', '.png'): #print(img) pdf1.add_page() pdf1.image(img) pdf1.output("images.pdf", "F")
Code language: Python (python)

Python script 3: Add image to existing PDF file in Python using reportlab

from reportlab.pdfgen import canvas def create_pdf(): pdf_file = 'multipage.pdf' can = canvas.Canvas(pdf_file) can.drawString(20, 800, "First Page") can.showPage() can.drawString(20, 800, "Second Page") can.showPage() can.drawString(20, 700, "Third Page") can.showPage() can.save() def add_image(): from PyPDF2 import PdfFileWriter, PdfFileReader import io in_pdf_file = 'multipage.pdf' out_pdf_file = 'with_image.pdf' img_file = '../../static/img/code_maven_440x440.png' packet = io.BytesIO() can = canvas.Canvas(packet) #can.drawString(10, 100, "Hello world") x_start = 0 y_start = 0 can.drawImage(img_file, x_start, y_start, width=120, preserveAspectRatio=True, mask='auto') can.showPage() can.showPage() can.showPage() can.save() #move to the beginning of the StringIO buffer packet.seek(0) new_pdf = PdfFileReader(packet) # read the existing PDF existing_pdf = PdfFileReader(open(in_pdf_file, "rb")) output = PdfFileWriter() for i in range(len(existing_pdf.pages)): page = existing_pdf.getPage(i) page.mergePage(new_pdf.getPage(i)) output.addPage(page) outputStream = open(out_pdf_file, "wb") output.write(outputStream) outputStream.close() create_pdf() add_image()
Code language: Python (python)

Python script 4: Place image over PDF

Install dependencies

pip install reportlab pip install pypdf2
from reportlab.pdfgen import canvas from PyPDF2 import PdfFileWriter, PdfFileReader # Create the watermark from an image c = canvas.Canvas('watermark.pdf') # Draw the image at x, y. I positioned the x,y to be where i like here c.drawImage('test.png', 15, 720) # Add some custom text for good measure c.drawString(15, 720,"Hello World") c.save() # Get the watermark file you just created watermark = PdfFileReader(open("watermark.pdf", "rb")) # Get our files ready output_file = PdfFileWriter() input_file = PdfFileReader(open("test2.pdf", "rb")) # Number of pages in input document page_count = input_file.getNumPages() # Go through all the input file pages to add a watermark to them for page_number in range(page_count): print "Watermarking page {} of {}".format(page_number, page_count) # merge the watermark with the page input_page = input_file.getPage(page_number) input_page.mergePage(watermark.getPage(0)) # add page from input file to output document output_file.addPage(input_page) # finally, write "output" to document-output.pdf with open("document-output.pdf", "wb") as outputStream: output_file.write(outputStream)
Code language: Python (python)

Python script 5:

# -*- coding: utf-8 -*- from io import BytesIO from PyPDF2 import PdfFileWriter, PdfFileReader from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import A4 def gen_pdf(): # there are 66 slides (1.jpg, 2.jpg, 3.jpg...) path = 'slades/{0}.jpg' pdf = PdfFileWriter() for num in range(1, 67): # for each slide # Using ReportLab Canvas to insert image into PDF imgTemp = BytesIO() imgDoc = canvas.Canvas(imgTemp, pagesize=A4) # Draw image on Canvas and save PDF in buffer imgDoc.drawImage(path.format(num), -25, -45) # x, y - start position # in my case -25, -45 needed imgDoc.save() # Use PyPDF to merge the image-PDF into the template pdf.addPage(PdfFileReader(BytesIO(imgTemp.getvalue())).getPage(0)) pdf.write(open("output.pdf","wb")) if __name__ == '__main__': gen_pdf()
Code language: Python (python)

Python script 6:

from PyPDF2 import PdfFileWriter, PdfFileReader def watermarks(temp, watermar,new_file): template = PdfFileReader(open(temp, 'rb')) wpdf = PdfFileReader(open(watermar, 'rb')) watermark = wpdf.getPage(0) for i in xrange(template.getNumPages()): page = template.getPage(i) page.mergePage(watermark) output.addPage(page) with open(new_file, 'wb') as f: output.write(f)
Code language: JavaScript (javascript)

Python script 7: Adding a watermark to a multi-page PDF

# !/usr/bin/python # Adding a watermark to a multi-page PDF from pdfrw import PdfReader, PdfWriter, PageMerge input_file = "example.pdf" output_file = "example-drafted.pdf" watermark_file = "barcode.pdf" # define the reader and writer objects reader_input = PdfReader(input_file) writer_output = PdfWriter() watermark_input = PdfReader(watermark_file) watermark = watermark_input.pages[0] # go through the pages one after the next for current_page in range(len(reader_input.pages)): merger = PageMerge(reader_input.pages[current_page]) merger.add(watermark).render() # write the modified content to disk writer_output.write(output_file, reader_input)
Code language: Python (python)

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts