Convert Images to PDF using Python

In this short guide, you’ll see how to convert images to PDF using Python. The PIL package will be used to accomplish this goal.

To begin, here is a template that you can use to convert a png image to PDF using Python (for JPEG, use the file extension of ‘jpg’):

from PIL import Image

image1 = Image.open(r'path where the image is stored\file name.png')
im1 = image1.convert('RGB')
im1.save(r'path where the pdf will be stored\new file name.pdf')

Later, you’ll also see how to convert a list of images to PDF.

Steps to Convert Images to PDF using Python

Step 1: Install the PIL package

To start, install the PIL package using the command below:

pip install Pillow

If you’re a Windows user, you may follow this guide for the instructions to install a package using pip.

Step 2: Capture the path where your image is stored

Next, capture the path where your image is stored. In my case, the image is stored under the following path:

C:\Users\Ron\Desktop\Test

path with file

Step 3: Convert the image to PDF using Python

For the final step, you can use the template below in order to convert the image to PDF:

from PIL import Image

image1 = Image.open(r'path where the image is stored\file name.png')
im1 = image1.convert('RGB')
im1.save(r'path where the pdf will be stored\new file name.pdf')

In our example, I’ll be storing the PDF file under the same path where the original image is stored (from Step 2):

C:\Users\Ron\Desktop\Test

In addition:

  • My original image file name is image1
  • While the new PDF file name will be myFirstImage

So this is the full Python code that I used to convert the image to PDF (you’ll need to adjust the paths to reflect the location where the files are stored on your computer):

from PIL import Image

image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.png')
im1 = image1.convert('RGB')
im1.save(r'C:\Users\Ron\Desktop\Test\myFirstImage.pdf')

Run the code (adjusted to your paths), and the new PDF will be created at your specified location:

Convert Images to PDF using Python

The same principles apply if you have JPEG images (rather than png). In that case, the only thing you’ll need to change is the file extension to ‘jpg’:

from PIL import Image

image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.jpg')
im1 = image1.convert('RGB')
im1.save(r'C:\Users\Ron\Desktop\Test\myFirstImage.pdf')

Convert List of Images to PDF using Python

What if you have a list of images and you’d like to store all of them in a single PDF file?

For example, I added few more images under the following path:

C:\Users\Ron\Desktop\Test

image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.png')
image2 = Image.open(r'C:\Users\Ron\Desktop\Test\image2.png')
image3 = Image.open(r'C:\Users\Ron\Desktop\Test\image3.png')
image4 = Image.open(r'C:\Users\Ron\Desktop\Test\image4.png')

Next, I performed the conversion:

im1 = image1.convert('RGB')
im2 = image2.convert('RGB')
im3 = image3.convert('RGB')
im4 = image4.convert('RGB')

I then created a new imagelist (excluding the first image):

imagelist = [im2,im3,im4]

Finally, I used to following syntax to save the PDF (note the ‘im1’ at the beginning):

im1.save(r'C:\Users\Ron\Desktop\Test\myImages.pdf',save_all=True, append_images=imagelist)

Putting all the code components together:

from PIL import Image

image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.png')
image2 = Image.open(r'C:\Users\Ron\Desktop\Test\image2.png')
image3 = Image.open(r'C:\Users\Ron\Desktop\Test\image3.png')
image4 = Image.open(r'C:\Users\Ron\Desktop\Test\image4.png')
 
im1 = image1.convert('RGB')
im2 = image2.convert('RGB')
im3 = image3.convert('RGB')
im4 = image4.convert('RGB')

imagelist = [im2,im3,im4]

im1.save(r'C:\Users\Ron\Desktop\Test\myImages.pdf',save_all=True, append_images=imagelist)

Here is the new PDF that contains all the images:

Convert list of Images to PDF using Python

Tool to Convert Images to PDF

In the final section of this guide, I’ll share the code to create a tool to convert images to PDF.

This tool is based on the tkinter package which is used to create graphical user interfaces in Python:

from PIL import Image
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='File Conversion Tool', bg = 'lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def getFile ():
    global im1
    
    import_file_path = filedialog.askopenfilename()
    image1 = Image.open(import_file_path)
    im1 = image1.convert('RGB')

browseButton = tk.Button(text="     Select File     ", command=getFile, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton)

def convertToPdf ():
    global im1
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.pdf')
    im1.save(export_file_path)

saveAsButton = tk.Button(text='Convert to PDF', command=convertToPdf, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveAsButton)

def exitApplication():
    MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
    if MsgBox == 'yes':
       root.destroy()
     
exitButton = tk.Button (root, text='Exit Application',command=exitApplication, bg='brown', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=exitButton)

root.mainloop()

Run the code in Python, and you’ll see the following display:

tkinter GUI

Click on the ‘Select File‘ button:

Convert images to PDF using Python

Select the file (png or jpg) that you’d like to convert to PDF, and then press Open:

How to Convert images to PDF using Python

Click on the ‘Convert to PDF‘ button:

Tkinter display

Finally, type a name for your new PDF file (for example, ‘newImage’), and then click on Save:

Convert to PDF

Your new PDF file will be saved at your chosen location:

Convert images to PDF using Python