Open In App

Convert Images to PDFs using Tkinter – Python

Last Updated : 20 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 images 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 Module
from tkinter import *
  
# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')
  
  
# Add Labels and Buttons
Label(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 Tkinter
root.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 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))


Below is the implementation:

Python3




# 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()


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads