Today, you will create a project that will grab all of the image files from a particular directory and create a single PDF file including all of the images.
The interesting thing about Python is, you will need only 4 lines of code to achieve that! So, let's get started, shall we?
๐ก Detailed Article
I have written the complete article on freeCodeCamp. Make sure to read that too!
๐บ Video Guideline
I know that many of you like to watch video instead of following a complete article. Fear not! I have also created a complete video tutorial just for you!
๐ฏ Pre Requisite
- Simply create a new folder for the project. Please make sure that you do not keep any spaces in the folder's name.
- Keep some image files in that directory. For this project, I am going to use the
.jpg
image files. Therefore I would suggest you to do the same thing! - You can download the royalty free images from Unsplash or Pexels. Keep in mind that our project can't handle large image files. Therefore, try to download those image files which are smaller in file size. You can select small files during downloading them from Unsplash.
- Now simply open the Visual Studio Code. If you want, then obviously you can use other text editors or IDEs. But for simplicity, I like to use widely used free software. And VS Code is the best in this sector.
- Create a Python file where you will write down the entire code with me. I am going to create a file named
Script.py
. - Install the package/library named
img2pdf
. This library is used for converting images to PDF via direct JPEG inclusion. You can check this website for more details. I am going to install it using pip. Simply open a terminal and use the command,pip install img2pdf
.
๐ป Code
import os
import img2pdf
with open("ImageContainingBook.pdf", "wb") as file:
file.write(img2pdf.convert([i for i in os.listdir("C:\\Users\\fahim\\Desktop\\ImageToPdf") if i.endswith(".jpg")])) # Change the file directory accordingly
โญ The complete project is available on GitHub.
Here, I imported two necessary packages first, os
and img2pdf
. Then I need to specify exactly in which file format and file name I want to place my image files. As I want to generate a PDF file to include all of my image files, I have specified that.
Then I need to specify what I want to do with the file. I want to write in that file and I want the conversion functionality of the img2pdf
library. In my specified directory, there might be a lot of different files and that is natural. But as I only want to convert the image files which have a .jpeg
extension in them, I need to specify that explicitly. Also, I definitely need to include the file directory where it will get all of the images.
๐โโ๏ธ Run
If you have the Code Runner extension installed on your VS Code, then you can simply run the file using that extension.
As my filename is Script.py and I am using my Windows machine, my command would be python Script.py
. If you are a Linux or Mac user, then use the command python3 Script.py
or python3 filename.py
.
๐โโ๏ธ Conclusion
I hope you have enjoyed this short article. ๐
If you have any questions then please let me know using Twitter or LinkedIn.
You can also follow me on:
๐GitHub: FahimFBA
๐YouTube: @FahimAmin
If you are interested then you can also check my website: https://fahimbinamin.com/
Have a great day! ๐
๐ผ๏ธ Cover: Photo by Christina Morillo: https://www.pexels.com/photo/woman-programming-on-a-notebook-1181359/
Top comments (2)
We have Spire.PDF to convert image to PDF in Java, C#/VB.NET and C++. Would you mind to share an article of it?
I'd love to do that! Would you mind sharing the relevant docs or something like that for me to analyze that?