Following code shows you how to convert a set of JPEG images to a single PDF file.
This was tested on windows 10. You need the following libraries installed before everything.
- pdf2image (https://pypi.org/project/pdf2image/)
- PyFPDF (https://pyfpdf.readthedocs.io/en/latest/)
- Pillow (https://pillow.readthedocs.io/en/stable/index.html)
Make sure your script and the images are in the same location.
from pdf2image import convert_from_path
import os
from fpdf import FPDF
from PIL import Image
pdfFileName = 'Your_file_name_without_extension'
def makePdf(pdfFileName, dir = ''):#Take the image list and convert them to a single pdf
fileList = []
for file in os.listdir("."):
if file.endswith(".jpg"):
fileList.append(file)
if (dir):
dir += "/"
cover = Image.open(dir + str(fileList[0]))
width, height = cover.size
pdf = FPDF(unit = "pt", format = [width, height])
for page in fileList:
pdf.add_page()
pdf.image(dir + str(page), 0, 0)
pdf.output(dir + pdfFileName + ".pdf", "F")
makePdf(pdfFileName, dir = '')
Convert a set of images to a PDF document