Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Tkinter Project to Convert Multiple Images to PDF Document Using img2pdf Library GUI Desktop App

Posted on November 16, 2022

Welcome folks today in this blog post we will be converting multiple images to pdf document using img2pdf library in tkinter. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

 

pip install img2pdf

 

 

And after that you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
# Import Module
from tkinter import *
from tkinter.filedialog import askopenfilenames
import img2pdf
 
# Create Object
root = Tk()
# set Geometry
root.geometry('400x200')
 
# Execute Tkinter
root.mainloop()

 

 

As you can see we are making the new window of tkinter. And in this we are using the geometry method to set the width and height of the window. And then we are importing the img2pdf library and also we are importing the filedialog library. And then we are starting the tkinter app using the mainloop() method.

 

 

 

 

Adding the Label and Buttons

 

 

Now we will be adding the labels and the buttons inside the tkinter window.

 

 

Python
1
2
3
4
5
6
# Add Labels and Buttons
Label(root, text = "IMAGE CONVERSION",
font = "italic 15 bold").pack(pady = 10)
 
Button(root, text = "Select Images",
command = select_file, font = 14).pack(pady = 10)

 

 

And in the above code we are using the label widget and also we are using the button widget to add the text attribute and also we are attaching the command to the button and also setting the font of the button. And also we are setting the text property of the Label widget. And we need to write the select_file() method to select image files from the popup window.

 

 

 

 

Now we will be adding two more buttons inside the frame to allow users to convert multiple images or single image to pdf document. For this we need to copy paste the below code

 

 

PowerShell
1
2
3
4
5
6
7
8
9
10
11
frame = Frame()
frame.pack(pady = 20)
 
Button(frame, text = "Image to PDF",
command = image_to_pdf,
relief = "solid",
bg = "white", font = 15).pack(side = LEFT, padx = 10)
 
Button(frame, text = "Images to PDF",
command = images_to_pdf, relief = "solid",
bg = "white", font = 15).pack()

 

 

As you can see we are adding the frame widget and then we are adding the button widget where we are adding single image to pdf and multiple images to pdf button inside the frame.

 

 

 

 

 

Selecting Image Files From Select File Popup Window

 

 

Now we will be adding the method to allow the users to select the image files as shown below

 

 

Python
1
2
3
4
def select_file():
global file_names
file_names = askopenfilenames(initialdir = "/",
title = "Select File")

 

 

As you can see inside the above method we are first of declaring the global variable file_names and then we are using the askopenfilenames() method to allow the user to select images from the directory. Here in this method we can select single or multiple images from the directory.

 

 

Converting Single Image to PDF Document

 

 

Now we will be writing the method to convert the single image to pdf document using the img2pdf library as shown below

 

 

Python
1
2
3
4
5
# IMAGE TO PDF
def image_to_pdf():
for index, file_name in enumerate(file_names):
with open(f"file {index}.pdf", "wb") as f:
f.write(img2pdf.convert(file_name))

 

 

As you can see we are first of all checking if single image is present and then we are using the for loop to get the actual file name and then opening that image in read mode and then we are converting the image to pdf document using the convert() method of the img2pdf library. And in the argument we are passing the image filename.

 

 

Converting Multiple Images to PDF Document

 

 

Now we will be writing the function to convert multiple images selected by the user to the pdf document using the img2pdf library as shown below

 

 

Python
1
2
3
4
# IMAGES TO PDF
def images_to_pdf():
with open(f"file.pdf", "wb") as f:
f.write(img2pdf.convert(file_names))

 

 

Here in this function we are using the open method to get all the image files selected by the user. And then we are using the convert() method of the img2pdf library to convert all the images to pdf document.

 

 

 

 

Full Source Code

 

 

Wrapping the blog post this is the full source code of the app.py file as shown below

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Import Module
from tkinter import *
from tkinter.filedialog import askopenfilenames
import img2pdf
 
# Create Object
root = Tk()
# set Geometry
root.geometry('400x200')
 
def select_file():
global file_names
file_names = askopenfilenames(initialdir = "/",
title = "Select File")
 
# IMAGE TO PDF
def image_to_pdf():
for index, file_name in enumerate(file_names):
with open(f"file {index}.pdf", "wb") as f:
f.write(img2pdf.convert(file_name))
 
# IMAGES TO PDF
def images_to_pdf():
with open(f"file.pdf", "wb") as f:
f.write(img2pdf.convert(file_names))
 
# Add Labels and Buttons
Label(root, text = "IMAGE CONVERSION",
font = "italic 15 bold").pack(pady = 10)
 
Button(root, text = "Select Images",
command = select_file, font = 14).pack(pady = 10)
 
frame = Frame()
frame.pack(pady = 20)
 
Button(frame, text = "Image to PDF",
command = image_to_pdf,
relief = "solid",
bg = "white", font = 15).pack(side = LEFT, padx = 10)
 
Button(frame, text = "Images to PDF",
command = images_to_pdf, relief = "solid",
bg = "white", font = 15).pack()
 
# Execute Tkinter
root.mainloop()

Recent Posts

  • Node.js OfficeGen Example to Generate Excel Files By Adding Data inside Cells & Sheets in Javascript
  • Node.js OfficeGen Example to Create Word Docx Files & Add Text Images inside it Using Javascript
  • React.js Twitter API Tutorial to Embed Profile & Timeline, Hashtags of User in Browser Using Javascript
  • Android Java Tutorial to Change Styles & Visibility of System Bars (Top, Action & Status) Full Example
  • Android Java Project to Display & Play Audio Files From Storage inside ListView Using MediaPlayer Class
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme