Skip to content

Coding Shiksha

Menu
  • Home
  • Youtube Channel
  • Online Store
  • Online Tools
  • Donate Us
  • Sitemap
  • Privacy Policy
Menu

Python 3 Pillow Script to Convert All Images in Directory to PDF Document Using Glob Module in Command Line

Posted on October 28, 2022

Welcome folks today in this blog post we will be converting all the images in directory to pdf document using glob module and pillow library in python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below libraries for this application as shown below

 

 

pip install pillow

 

 

As you can see we are installing the pillow library for the image processing and converting to pdf document. Now you need to create the directory called images where we will be storing all the input images that we need to export to pdf document.

 

The directory structure of the app is shown below

 

 

 

 

 

As you can see we have copied all the images inside the images directory and we also need to create the app.py file where we will be writing the code to export the images to pdf document.

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
from PIL import Image
from glob import glob
import os
 
 
files = glob("images/*.png")
iml = []
print(f"{files=}")

 

 

As you can see we are importing the glob module at the top. This module is required for targeting all the images files which is stored inside the images folders by the path at once using the * operator. And also we are declaring an output empty array.

 

 

And then guys we need to sort all these image files in the ascending order using the sort() function in python.

 

Python
1
files = sorted(files)

 

 

And after that guys we will be using a simple for loop to loop through all the images and in each iteration we are first of all opening the image in memory and then we are converting that image to RGB Mode and then appending it to the output array.

 

 

Python
1
2
3
4
for img in files:
    imgs = Image.open(img)
    rgb_im = imgs.convert('RGB') # to prevent errors: cannot save mode RGBA
    iml.append(rgb_im)

 

 

And after that we will be saving the output pdf file for that we are first of all initializing the output filename and after that we are saving it to local disk using the os built in python module. And inside the save() method as you can see below we are passing some options which will be first of all the quality of resolution of images here you can set the value from 0 to 100. And then we are passing the output array which contains all the images in rgb mode and also we are setting the save_all boolean attribute to true to save all the images.

 

 

Python
1
2
3
4
5
6
pdf = "ALL.pdf"
print(iml)
image = iml[0]
iml.pop(0)
image.save(pdf, "PDF" , resolution=100.0, save_all=True, append_images=iml)
os.system(pdf)

 

 

 

 

 

As you can see all the seven images that were stored is successfully converted to pdf document.

 

 

Full Source Code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from PIL import Image
from glob import glob
import os
 
 
files = glob("images/*.png")
iml = []
print(f"{files=}")
files = sorted(files)
for img in files:
    imgs = Image.open(img)
    rgb_im = imgs.convert('RGB') # to prevent errors: cannot save mode RGBA
    iml.append(rgb_im)
pdf = "ALL.pdf"
print(iml)
image = iml[0]
iml.pop(0)
image.save(pdf, "PDF" , resolution=100.0, save_all=True, append_images=iml)
os.system(pdf)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

©2023 Coding Shiksha | Design: Newspaperly WordPress Theme