The Wayback Machine - https://web.archive.org/web/20241123184034/https://www.geeksforgeeks.org/convert-images-to-pdf-using-tkinter-python/
Open In App

Convert Images to PDFs using Tkinter – Python

Last Updated : 20 Jan, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:



Previous Article
Next Article

Similar Reads

Downloading PDFs with Python using Requests and BeautifulSoup
BeautifulSoup object is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. The BeautifulSoup object represents the parsed document as a whole. For most purposes, you can treat it as a Tag object. Requests library i
2 min read
Parsing PDFs in Python with Tika
Apache Tika is a library that is used for document type detection and content extraction from various file formats. Using this, one can develop a universal type detector and content extractor to extract both structured text and metadata from different types of documents such as spreadsheets, text documents, images, PDF's, and even multimedia input
2 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Rename a folder of images using Tkinter
Prerequisites: Python GUI – tkinter, os.listdir() method, os.rename() method In this article, the task is to rename a batch of images using Python. Now say given n images in a folder having random names. For example, consider the images below. Now the requirement is to rename them in ordered fashion and appending their original dimensions of respec
3 min read
Loading Images in Tkinter using PIL
In this article, we will learn how to load images from user system to Tkinter window using PIL module. This program will open a dialogue box to select the required file from any directory and display it in the tkinter window.Install the requirements - Use this command to install Tkinter : pip install python-tkUse this command to install PIL : pip i
3 min read
Reading Images With Python - Tkinter
There are numerous tools for designing GUI (Graphical User Interface) in Python such as tkinter, wxPython, JPython, etc where Tkinter is the standard Python GUI library, it provides a simple and efficient way to create GUI applications in Python. Reading Images With Tkinter In order to do various operations and manipulations on images, we require P
2 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
How to Use Bitmap images in Button in Tkinter?
Prerequisite: Python GUI – tkinter 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. A bitmap is an array of binary data representing the values of pixels in an image. A GIF i
2 min read
How to Dynamically Resize Background Images - Tkinter
You might surely be worried about how to resize your background image with the change in screen size. In this article, We are creating an advanced-level GUI application using the Tkinter module using Python in which you need to resize the app window various times and dynamically resize background images. Modules Required: Tkinter: As the GUI applic
6 min read
How To Use Images as Backgrounds in Tkinter?
Prerequisite: Python GUI – tkinter , Frame In this article, We are going to write a program use image in the background. In Tkinter, there is no in-built function for images, so that it can be used as a background image. It can be done with various methods: Method 1: Using photoimage methods. When it comes to GUI based application, images play a vi
3 min read
How to Display Images within Tkinter Frames
Tkinter is a Python module used to create Graphical User Interface (GUI) applications. Tkinter has many widgets such as Button, Label, Text, Entry, Frame, Checkbox and more that make it easy and quick to develop desktop applications. How to Insert Image inside Frame in Tkinter?To insert an image inside a frame is the same as inserting an image in T
1 min read
Convert Audio to Video using Static Images in Python
In this article, we are going to convert an audio file(mp3) to a video file(mp4) using the images provided by the user to be shown during the duration of the video using Python. To do this, we will first convert the images to a GIF file and then combining with the audio file to produce the final video file. Packages Required Mutagen: This Python pa
5 min read
How to Convert images to NumPy array?
Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format. In this article we will see How to Convert images to NumPy array? Modules Needed:NumPy: By default in higher versions of
6 min read
Color game using Tkinter in Python
TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games. Let's try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this
4 min read
Python | Message Encode-Decode using Tkinter
Prerequisite : Basics of TkinterPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Python pr
5 min read
Simple registration form using Python Tkinter
Prerequisites: Tkinter Introduction, openpyxl module.Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let's make a simple information form GUI application using Tkinter. In this application, User has to fill up the required info
5 min read
Python | Simple GUI calculator using Tkinter
Prerequisite: Tkinter Introduction, lambda function Python offers multiple options for developing a 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 outputs the fastest and easiest way to create GUI a
6 min read
Python | Distance-time GUI calculator using Tkinter
Prerequisites : Introduction to Tkinter | Using google distance matrix API Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest wa
7 min read
Python | Random Password Generator using Tkinter
With growing technology, everything has relied on data, and securing this data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. An easy password can be hacked easily and all personal information can be misused. In order to prevent such things and keep the data safe, it is necessary to keep our passwords
4 min read
How to visualize selection and insertion sort using Tkinter in Python?
In this article, we are going to create a GUI application that will make us visualize and understand two of the most popular sorting algorithms better, using Tkinter module. Those two sorting algorithms are selection sort and insertion sort. Selection sort and Insertion sort are the two most popular algorithms. Selection sort is a comparison-based
6 min read
Python | Real time currency converter using Tkinter
Prerequisites : Introduction to tkinter | Get the real time currency exchange ratePython 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 outputs the fastest and
4 min read
Python | Real time weather detection using Tkinter
Prerequisites : Introduction to tkinter | Find current weather of any cityPython 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 outputs the fastest and easiest
5 min read
Python | Simple FLAMES game using Tkinter
Prerequisites: Introduction to TkinterProgram to implement simple FLAMES game Python offers multiple options for developing a 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 outputs the fastest and e
5 min read
Python | Simple calculator using Tkinter
Prerequisite : Tkinter IntroductionPython 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 outputs the fastest and easiest way to create the GUI applications. Cr
3 min read
Python | Create a digital clock using Tkinter
As we know Tkinter is used to create a variety of GUI (Graphical User Interface) applications. In this article we will learn how to create a Digital clock using Tkinter. Prerequisites: Python functions Tkinter basics (Label Widget) Time module https://www.youtube.com/watch?v=1INA9AmaDtQ Using Label widget from Tkinter and time module: In the follow
2 min read
Python | Loan calculator using Tkinter
Prerequisite: Tkinter Introduction 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 outputs the fastest and easiest way to create GUI applications. Creati
5 min read
Getting screen's height and width using Tkinter | Python
Tkinter provides some methods with the help of which we can get the current screen height and width.Following methods can be used to decide height and width : winfo_screenheight() // Returns screen height in pixels winfo_screenmmheight() // Returns screen height in mm winfo_screenwidth() // Returns screen width in pixels winfo_screenmmwidth() // Re
1 min read
Python Tkinter | Moving objects using Canvas.move() method
The Canvas class of Tkinter supports functions that are used to move objects from one position to another in any canvas or Tkinter top-level. Syntax: Canvas.move(canvas_object, x, y)Parameters: canvas_object is any valid image or drawing created with the help of Canvas class. To know how to create object using Canvas class take reference of this. x
2 min read
Python Tkinter | Create different type of lines using Canvas class
In Tkinter, Canvas.create_line() method is used to create lines in any canvas. These lines can only be seen on canvas so first, you need to create a Canvas object and later pack it into the main window. Syntax: Canvas.create_line(x1, y1, x2, y2, ...., options = ...) Note: Minimum of 4 points are required to create a line, but you can also add multi
2 min read
Python Tkinter | Create different shapes using Canvas class
In Tkinter, Canvas class is used to create different shapes with the help of some functions which are defined under Canvas class. Any shape that Canvas class creates requires a canvas, so before creating any shapes a Canvas object is required and needs to be packed to the main window. Canvas Methods for shapes: Canvas.create_oval(x1, y1, x2, y2, op
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg