Convert Images to PDF using Python

In this short guide, I’ll show you how to convert images to PDF using Python. I’ll use the PIL package 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, I’ll also show you 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, you’ll need to 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, my 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’ll need to 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, the path that I’ll be using to store the PDF file is the same as the path where the original image is stored (under 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 path), 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 would 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