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 may use to convert a png image to PDF using Python (for JPEG, use the file extension of ‘jpg’):

from PIL import Image

image_1 = Image.open(r'path where the image is stored\file name.png')
im_1 = image_1.convert('RGB')
im_1.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 (under Windows):

pip install Pillow

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.

For example, let’s suppose that a png image called ‘view_1‘ is stored under the following path:

C:\Users\Ron\Desktop\Test\view_1.png

Step 3: Convert the image to PDF using Python

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

from PIL import Image

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

For our example, the PDF file will be stored under the same path where the original image is stored (from Step 2).

Therefore, here is the full Python code to convert the image to PDF for our example (you’ll need to adjust the paths to reflect the location where the files will be stored on your computer):

from PIL import Image

image_1 = Image.open(r'C:\Users\Ron\Desktop\Test\view_1.png')
im_1 = image_1.convert('RGB')
im_1.save(r'C:\Users\Ron\Desktop\Test\view_1.pdf')

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

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

from PIL import Image

image_1 = Image.open(r'C:\Users\Ron\Desktop\Test\view_1.jpg')
im_1 = image_1.convert('RGB')
im_1.save(r'C:\Users\Ron\Desktop\Test\view_1.pdf')

Convert a 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, let’s add few more images under the same path:

image_1 = Image.open(r'C:\Users\Ron\Desktop\Test\view_1.png')
image_2 = Image.open(r'C:\Users\Ron\Desktop\Test\view_2.png')
image_3 = Image.open(r'C:\Users\Ron\Desktop\Test\view_3.png')
image_4 = Image.open(r'C:\Users\Ron\Desktop\Test\view_4.png')

Next, perform the conversion:

im_1 = image_1.convert('RGB')
im_2 = image_2.convert('RGB')
im_3 = image_3.convert('RGB')
im_4 = image_4.convert('RGB')

Then, create a new image_list (excluding the first image):

image_list = [im_2, im_3, im_4]

And finally, apply the following syntax to save the PDF (note the ‘im_1’ at the beginning):

im_1.save(r'C:\Users\Ron\Desktop\Test\my_images.pdf', save_all=True, append_images=image_list)

Putting all the code components together:

from PIL import Image

image_1 = Image.open(r'C:\Users\Ron\Desktop\Test\view_1.png')
image_2 = Image.open(r'C:\Users\Ron\Desktop\Test\view_2.png')
image_3 = Image.open(r'C:\Users\Ron\Desktop\Test\view_3.png')
image_4 = Image.open(r'C:\Users\Ron\Desktop\Test\view_4.png')

im_1 = image_1.convert('RGB')
im_2 = image_2.convert('RGB')
im_3 = image_3.convert('RGB')
im_4 = image_4.convert('RGB')

image_list = [im_2, im_3, im_4]

im_1.save(r'C:\Users\Ron\Desktop\Test\my_images.pdf', save_all=True, append_images=image_list)

Once you run the code (adjusted to your paths), you’ll get a single PDF that contains all the images.