Combine all the images in a folder to a PDF file

When you have a series of images which needs to be combined as a single PDF file for ease of reading, use the following python script. The script used pillow library.

If you don’t have pillow installed already, type the following in your terminal to install it.

python -m pip install Pillow

for more information follow this link

The script

from PIL import Image
import os

folder = os.getcwd()
files= os.listdir(folder)


for file in files:
    if file.endswith(".png"): #replace ".png" with ".jpg" if you have jpg file format,image))

pdf_path = "PDF_Filename.pdf" #include the ".pdf" extension

images[0].save(pdf_path, "PDF", resolution=100.0, save_all=True, append_images=images[1:])

# Note the save_all=True, and append_images = [1:]. These two arguments will make sure 
# all the images are included and saved together

Note that, the script isn’t controlling the page size of the PDF. It just flattens the images and combines them as a pdf file. So the page size in the pdf file will be the size of the images. To control the page size of the PDF file (Standard sizes like A4, A3), we need to use a different library. I will link the post here when I write it.