Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Convert Images to PDFs using Tkinter
Python is a scripting language that helps create various file converters. Using the img2pdf module and Tkinter, we can build a GUI application that converts multiple images into PDF format. The img2pdf library efficiently parses image data and converts it to PDF without quality loss.
Installation
First, install the required module ?
pip install img2pdf
Building the Image to PDF Converter
We will create a Tkinter application with file selection dialog and conversion functionality ?
from tkinter import *
from tkinter import filedialog, messagebox
from tkinter import ttk
import img2pdf
import os
# Create main window
win = Tk()
win.geometry('750x300')
win.title("Image to PDF Converter")
# Global variable to store selected images
images = []
def select_files():
global images
images = filedialog.askopenfilenames(
title="Select Image Files",
filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp *.tiff")]
)
if images:
file_label.config(text=f"Selected {len(images)} image(s)")
else:
file_label.config(text="No files selected")
def convert_to_pdf():
if not images:
messagebox.showerror("Error", "Please select image files first!")
return
try:
# Ask user where to save the PDF
output_path = filedialog.asksaveasfilename(
title="Save PDF as",
defaultextension=".pdf",
filetypes=[("PDF files", "*.pdf")]
)
if output_path:
# Convert images to PDF
with open(output_path, "wb") as pdf_file:
pdf_file.write(img2pdf.convert(images))
messagebox.showinfo("Success", f"PDF saved as {os.path.basename(output_path)}")
status_label.config(text="Conversion completed successfully!")
except Exception as e:
messagebox.showerror("Error", f"Conversion failed: {str(e)}")
# Create GUI elements
title_label = Label(win, text="Image to PDF Converter", font=("Arial", 20, "bold"))
title_label.pack(pady=20)
select_btn = ttk.Button(win, text="Select Images", command=select_files)
select_btn.pack(pady=10)
file_label = Label(win, text="No files selected", fg="gray")
file_label.pack(pady=5)
convert_btn = ttk.Button(win, text="Convert to PDF", command=convert_to_pdf)
convert_btn.pack(pady=10)
status_label = Label(win, text="", fg="green")
status_label.pack(pady=10)
win.mainloop()
Key Features
- File Selection: Users can select multiple image files using a dialog box
- Format Support: Supports common image formats like JPG, PNG, BMP, TIFF
- Save Dialog: Allows users to choose where to save the output PDF
- Error Handling: Displays helpful error messages for common issues
- Status Updates: Shows feedback about selected files and conversion status
How It Works
The application works in these steps:
-
File Selection:
filedialog.askopenfilenames()opens a dialog to select multiple images - Validation: The app checks if files are selected before conversion
-
Save Location:
filedialog.asksaveasfilename()lets users choose where to save the PDF -
Conversion:
img2pdf.convert()converts all selected images into a single PDF -
Feedback: Success or error messages are displayed using
messagebox
Conclusion
This Tkinter application provides an easy way to convert multiple images into a single PDF file. The img2pdf library ensures high-quality conversion while the GUI makes it user-friendly for non-programmers.
Advertisements
