DEV Community

Cover image for Create A PDF From Multiple Images Using Python
Rizwan Hasan for TechLearners

Posted on • Updated on

Create A PDF From Multiple Images Using Python

In today's world PDF is a very popular file format among book readers, students, and officials, etc. Those who read books on their mobile phones or PC(Personal Computers) know the importance of PDF format in everyday life and many times need to make a single PDF from a bunch of images in order to share and read. As an example, it turned out to be a life-saving thing for me a couple of days ago. I love reading Manga. So, I downloaded a popular manga as a .zip file, after extracting there were 144 chapters as 144 folders and each folder has 19 to 32 images. So, I thought if I convert every chapter into a single PDF then it will be a lot easier to read and organize. I am going to share today how I did this thing easily using Python Programming Language.


PDF


It happens often that our friends send us some class notes as images on Messenger or WhatsApp. Instead of reading those as images, it’s more convenient to make a PDF and read it. Many people love reading comics and mangas too. Like me, they download episode images and read them. But also creating a PDF is a more convenient solution if you’re going to read it offline. Moreover, developers need this too. Downloading multiple images from websites or web-scraping many times ended up creating PDFs from those scrapped images. In such cases where you need to create a PDF from a bunch of images one time then it’s good to go with free software available on the internet. But if you need to do it more than 10 times or I say 100 times respectively, then isn’t it annoying? Strongly, you need a solution here but instead of finding a solution on ready-made software, why not you make your own solution? I ended up writing a couple of lines in Python to achieve the goal I’ve told you about earlier.


Code

Explanation

The code is very straight forward. I’ve taken five images and created an object of that images using the PIL library’s Image module. To create PDF, the image must need to have in RGB mode. So, I converted their mode to RGB. Now comes the tricky part. I wish to create a specific order of five images in PDF and that’s why I need to specify the order of images. For that, I created a Python list and on the list, I put them in my wished order. I want to create an order like this "image1, image2, image3, image4, image5" but I didn’t add the image1 in the list because if I do that then the image1 will be shown 2 times. It will show 2 times because when I’m creating the PDF, I started it just by adding the image1. So, if the image1 is already in the list and for PDF creation if I use the save option from image1 then it means image1 is already in the list and also adding when creating the PDF. For this reason, never add the first image of your desire list of images ordered.





Best of Luck

Share your opinion in the discussion section below and of course the questions if any. Don't forget to follow us.

💡 AND SUBSCRIBING to our YouTube TechLearnersInc and Telegram t.me/TechLearners will be amazing.


📌 Featured in DEV Community's Twitter

Top comments (0)