DEV Community

Cover image for How To Convert Multiple Images Into A Single PDF File Using 4 Lines of Python Code
Md. Fahim Bin Amin
Md. Fahim Bin Amin

Posted on • Updated on

How To Convert Multiple Images Into A Single PDF File Using 4 Lines of Python Code

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!

Python Project โ€“ How to Convert Multiple Images into a Single PDF File

Creating projects is the best way to learn a programming language. It is fun and it's a creative way to learn new things. Whenever I try to learn a new language or new technology, I try to create a project, whether it's a small byte-sized or big pro...

favicon freecodecamp.org

๐Ÿ“บ 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

  1. Simply create a new folder for the project. Please make sure that you do not keep any spaces in the folder's name.
  2. 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!
  3. 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.
  4. 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.
  5. Create a Python file where you will write down the entire code with me. I am going to create a file named Script.py.
  6. 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
Enter fullscreen mode Exit fullscreen mode

โญ 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)

Collapse
 
annagraz profile image
AnnaGraz

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?

Collapse
 
fahimfba profile image
Md. Fahim Bin Amin

I'd love to do that! Would you mind sharing the relevant docs or something like that for me to analyze that?