How to Create a PDF File from a List of Images with Python

Written by binita | Published 2021/02/17
Tech Story Tags: python | how-to-guide | programming | pdf | coding | python-programming | learn-to-code-python | learn-to-code

TLDR How to Create a PDF File from a List of Images with Python? How to create a PDF file from a list of images with Python. The power of programming is more exploited in batch processing and automation, says Binita Binita Gyawali. Binita is a junior data scientist at AID at the AID and is a senior data scientist in the U.S. He uses Python to create PDF files from images of his transcript sheets. He uses the fpdf library of Python to export the PDF file content.via the TL;DR App

I had received the transcripts of my Bachelor's degree around a month ago. I always visualized transcripts as one document consisting of marksheets of all the semesters, but, when I received my transcript, I had four sheets. That was annoying for me, as now I had to do multiple scans to upload my transcript somewhere. To make such an upload easier, I can scan all my sheets one time and create a PDF file from images of those sheets. That will make my life easy with transcript uploads, won't that?
So, for creating a PDF file from a bunch of images, I know of two ways.
  1. I can use web services for such usecase and upload images of my sheets
  2. Use some programming language to do the task
I, a lover of programming and scripting, am inclined to do the latter way. Let's use Python for the task, shall we?
Using Python for this task will help when we have to do such conversion for hundreds of images, as uploading such a number of images manually is cumbersome and will take forever.
For this task, let's use fpdf library of Python.
import os
from fpdf import FPDF
pdf = FPDF()
pdf.set_auto_page_break(0)
pdf.set_auto_page_break(0) sets auto page break to False. Setting auto page break to False is important, otherwise, we might have a blank page after each transcript sheet in the PDF.
Let's save the list of images in imagelist variable. Here, we are only allowing jpg extension files. If other extensions are to be incorporated, we can modify the below list comprehension syntax-ed code.
imagelist = [i for i in os.listdir(‘Images’) if i.endswith(‘jpg’)]
Now, we loop in the imagelist variable and add each image to the new page of the PDF file. We are specifying width and height manually to fit images on each page in the PDF file.
# imagelist is the list with all image filenames
for image in sorted(imagelist):
pdf.add_page()
pdf.image(‘Images/’ + image, w=190, h=280)
Note that we have sorted the list of images so as to allow desired sequences of images in the PDF file to be exported. Also, all the images are kept in the folder Images.
Finally, we export the PDF file content in the PDF file using the statement below. The argument "F" is used for saving the PDF file object to the local file.
pdf.output(“Transcript.pdf”, “F”)
This comes in handy if I have to do the conversion in a batch. The power of programming is more exploited in batch processing and automation.
Did it motivate you to code in Python? Please let me know your feedback in the comment section. :)
Also published on: https://binitagyawali.medium.com/how-i-created-a-pdf-file-from-a-list-of-images-using-python-3fb3f2770e04

Written by binita | Data Girl
Published by HackerNoon on 2021/02/17