Image To PDF Maker
Documentation for the Image To PDF Maker
The provided code is a Python script that allows users to convert a collection of images into a PDF document. The user interface is built using the Tkinter library, which provides a simple graphical interface for the conversion process. The script offers three different conversion options:
Testing Text File : This option creates a text file named "img_files.txt" containing the list of image files sorted numerically by their filenames. It is useful for testing and verifying the correct sorting of images before converting them into a PDF.
PDF Conversion : This option converts the images into a PDF document with adjustable DPI (dots per inch) settings for different output qualities. The user can select from three DPI options: Screen (72 DPI), Ebook (150 DPI), and Prepress (300 DPI). Higher DPI values result in higher quality PDFs with larger file sizes.
Normal Conversion: This option performs a standard conversion of the images into a PDF document without any DPI options. Images are sorted naturally by filename (using a custom sort key function) and then converted into a PDF at a fixed DPI of 300.
How the Code Works :
The script begins by importing necessary libraries, such as os, img2pdf, re, and tkinter, along with some specific modules from tkinter.
It defines several functions that handle different aspects of the image to PDF conversion process, including listing image files in a directory, converting images to PDF, and handling the user interface.
The main part of the script sets up the Tkinter GUI. It creates a window with labels, input fields, radio buttons, and buttons for user interaction. The user can select the image directory, choose the conversion option, set the image extension, and convert the images to PDF.
Depending on the selected conversion option, the corresponding function is called to perform the conversion. The conversion functions handle different aspects of the conversion process, such as sorting images, writing image filenames to a text file, converting images to PDF with different DPI settings, and saving the resulting PDF.
When the user clicks the "Convert" button, the on_convert_click function is executed, and it determines which conversion option is selected and calls the appropriate function.
How to Use the Script:
Run the EXE, and a window with the Image To PDF Maker GUI will appear.
Select the image directory by clicking the "Select Image Directory" button. The chosen directory will be displayed below.
Enter the image extension (e.g., "PNG", "JPEG") in the entry field. This extension will be used to filter the images in the selected directory for conversion.
Choose the desired conversion option by selecting the corresponding radio button:
"Testing Text File": This will create a text file containing the sorted list of image filenames.
"PDF Conversion": This will convert the images into a PDF document with selectable DPI options.
"Normal Conversion": This will convert the images into a PDF document using the default DPI of 300.
If you select "PDF Conversion," you can further choose the DPI option (Screen, E book, or Preprocess) from the nested radio buttons.
Click the "Convert" button to start the conversion process.
If you choose "Testing Text File" or "Normal Conversion," a file dialog will prompt you to select the destination where the converted PDF will be saved.
If you choose "PDF Conversion," a file dialog will prompt you to select the DPI option, and then another file dialog will prompt you to select the destination for the converted PDF.
After successful conversion, a message box will display the result ("Files stored in img_files.txt" or "Images converted to PDF successfully").
If any errors or issues occur during the conversion process, warning message boxes will appear to notify you.
Note:
The script is designed to handle common image formats like PNG, JPEG, etc. Make sure that you have the necessary image files in the selected directory with the appropriate extension for the conversion to work correctly.