Nerd For Tech
Published in

Nerd For Tech

Save Multiple Pictures As a PDF File, a.k.a Comics Image to PDF

Photo by Erik Mclean on Unsplash

To be honest, this story is handy for me, and that's why I write this story. Okay, this story came when I was training with a famous corporate worldwide—haha, kidding. I found the course material was a collection of images, and because I don't always go online, I want to download it as a pdf. This problem could be easily by pressing ctrl+j and save as a pdf. But, I want to focus on the image without any distraction. Finally, I downloaded all the pictures on that site, but a new question came.

How to combine all pictures into one pdf files?

After struggling a few times, I finally found how to solve this problem. But, because I think it was not wise to share the materials here, I got to think of another question and found this one.

How to implement it on comics site? How to combine all pictures on an online comic as a pdf so it could easily bring anywhere at anytime?

Sorry for all those questions ✌. And yeah, I could answer the question. Let's go to the flow section.

The flow

There are several online comic sites for free. But for example, let's go to Read One Piece Manga Online. As you read, maybe you understand that we are trying to implement it in one piece comic site. Do you like one piece?. Okay, the flow is like this.

  1. We will open a site that contains comics on it.
  2. Using the fatkun batch downloader, we download all the images from the site. For more information about this, you can read my article here.
  3. Our program will combine all images and turn them into pdf.

The code

Here is the complete source code that I use.

from pathlib import Path
from PIL import Image
loc = input(“Folder location: “)imageList = []for p in Path(loc).glob(“*.[jpg][png]*”):
imageList.append(Image.open(f”{loc}\{p.name}”).convert(“RGB”))
im1 = imageList[0]
im2 = imageList[1::1]
filename = input(“What name of file to be saved: “)
im1.save(filename, save_all=True, append_images=im2)

Let's break it down. From the source code above, you can quickly see that I am importing two libraries pathlib and pillow. The pathlib library reads images based on the file's location, which pillow is used to preprocess the image. After that, we will ask the user to input the directory location and save it into loc variables. Also, we are declaring a list variable named imageList. Next, we read all formatted image files (jpg and png) within the folder that had been declared before.

The supported image is in RGB format to read the image as a pdf. To make sure the program works in every image format, such as grayscale, we convert it to RGB first. All the images are then saved in PDF format. To make it work, we take the first image inside imageList into the variable im1 and then saved the rest in the variable im2. The [1::1] mean we will read all the images from the first index until the rest.

Finally, our program will create a PDF formatted file from all images. The program will ask us the name of the file to us. After we type the image name, this action will finish our program, and the program will create the PDF.

The execution

To make it clear, watch the documentation below.

Conclusion

In this story, I talked about how to save multiple images as a PDF. I don't know who will use it. But, I believe there will be someone who finds this helpful article, maybe not now, but soon. Have a nice code!!!

Thanks for reading.

Source Code

I have already created a repository for this project. If you are interested in visiting the repo, here is the link.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Handhika Yanuar Pratama

Handhika Yanuar Pratama

A Stoic Developer || Junior Data Engineer || Dreamer