In today’s world PDF is a very popular file format among the 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.
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 episodes 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 PDF 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 for 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 earlier.
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 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 adding the image1. So, if the image1 is already in the list and for PDF creation if I use 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.
Code’s Repository: https://github.com/Rizwan-Hasan/Python-Image-to-PDF-convert
I’ve tried to give you the simplest code for creating PDF from a bunch of images.
If you liked my writing you can inspire me by giving a Clap. You can follow me on Medium and I’m also available on Twitter here. Don’t forget to share your opinion on the comment section below and also the questions if any.