Coders Packet

Image to Pdf convertor GUI application using Python Tkinter

By J Suhasini

The main aim of this project is to help any user convert multiple images into a single Portable Document Format (PDF) file.

Description of the project:

The Image to PDF converter has three buttons: Select Images, Select PDF and Convert. 

1. Select Images: This button opens a dialog box that allows you to select one or more image files (such as JPG, JPEG and PNG) that you wish to convert into a PDF.

2. Select PDF: By clicking this button a save file dialog will open where you can specify the desired name and location for the resulting PDF file.

3. Convert: When pressed this button triggers the "images_to_pdf" function. It takes the selected images and the chosen name for the PDF file as inputs and converts all the images into one PDF document.

The provided code utilizes the Python Tkinter library to create a Graphical User Interface (GUI) program in Python. Its purpose is to merge images into a PDF document.

The "images_to_pdf" function relies on the PIL library. It starts by opening the image in order to create a PDF with that image as its starting point. Then it proceeds to append all remaining images, onto this PDF document.
The function is also called upon to show a popup indicating success or to display an error message if the conversion fails.

After running the GUI users have the option to interact with the buttons and choose images and a PDF file. They can then convert the images into a PDF document.

Source Code:

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image
import os
# To convert images to PDF
def images_to_pdf(images, pdf_name):
    try:
        # create a new pdf file
        pdf = Image.open(images[0])
        images = [Image.open(image) for image in images]
        pdf.save(pdf_name, "PDF" ,resolution=100.0, save_all=True, append_images=images[1:])
        messagebox.showinfo("Success", "Images have been successfully converted to PDF.")
    except Exception as e:
        messagebox.showerror("Error", "Failed to convert images to PDF.\nError: " + str(e))
# To select images
def select_images():
    images = filedialog.askopenfilenames(title="Select Images", filetypes=(("Image files", "*.jpg;*.jpeg;*.png"),("All files", "*.*")), initialdir = "C:/")
    return images
# To select pdf name and path
def select_pdf():
    pdf = filedialog.asksaveasfilename(title="Save PDF As", defaultextension=".pdf", initialdir = "C:/", filetypes=(("PDF files", "*.pdf"),("All files", "*.*")))
    return pdf
# create GUI
root = tk.Tk()
root.title("Convert Images to PDF")
select_images_btn = tk.Button(root, text="Select Images", command=select_images)
select_pdf_btn = tk.Button(root, text="Select PDF", command=select_pdf)
convert_btn = tk.Button(root, text="Convert", command=lambda: images_to_pdf(select_images(), select_pdf()))
select_images_btn.pack()
select_pdf_btn.pack()
convert_btn.pack()
root.mainloop()
 
Output:

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by J Suhasini (Suhasini)

Download packets of source code on Coders Packet