Prerequisite: Tkinter, img2pdf
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this article, we will learn how to convert Multiple mages to Multiple PDFs or Multiple Images to One PDF Using Tkinter in Python. For conversion, we will use the img2pdf Module.
img2pdf is an open-source Python package to convert images to pdf format. It includes another module Pillow which can also be used to enhance the image (Brightness, contrast, and other things)
For installation runs this command into your terminal:
pip install img2pdf
Let’s Understand step by step implementation:
Step 1: Create a Tkinter window and add Buttons, Labels, etc…
Python3
# Import Modulefrom tkinter import * # Create Objectroot = Tk() # set Geometryroot.geometry('400x200') # Add Labels and ButtonsLabel(root, text = "IMAGE CONVERSION", font = "italic 15 bold").pack(pady = 10) Button(root, text = "Select Images", font = 14).pack(pady = 10) frame = Frame()frame.pack(pady = 20) Button(frame, text = "Image to PDF", relief = "solid", bg = "white", font = 15).pack(side = LEFT,padx = 10) Button(frame, text = "Images to PDF", relief = "solid", bg = "white",font = 15).pack() # Execute Tkinterroot.mainloop() |
Step 2: We will create three functions; select_file(), image_to_pdf(), images_to_pdf()
- select_file: It will help you to select the file
- image_to_pdf: It is used for converting one image file to one pdf file
- images_to_pdf: It is used for converting Multiple image files to one pdf file
Python3
def select_file(): global file_names file_names = askopenfilenames(initialdir = "/",title = "Select File") # IMAGE TO PDFdef 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 PDFdef images_to_pdf(): with open(f"file.pdf","wb") as f: f.write(img2pdf.convert(file_names)) |
Below is the implementation:
Python3
# Import Modulefrom tkinter import *from tkinter.filedialog import askopenfilenamesimport img2pdf # Create Objectroot = Tk() # set Geometryroot.geometry('400x200') def select_file(): global file_names file_names = askopenfilenames(initialdir = "/", title = "Select File") # IMAGE TO PDFdef 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 PDFdef images_to_pdf(): with open(f"file.pdf", "wb") as f: f.write(img2pdf.convert(file_names)) # Add Labels and ButtonsLabel(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 Tkinterroot.mainloop() |
Output:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


