-
-
Notifications
You must be signed in to change notification settings - Fork 265
Added Image to PDF converter #1120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
ImageProcessingScripts/Image to PDF/Images/image1.png
Binary file added
ImageProcessingScripts/Image to PDF/Images/image2.jpeg
Binary file added
ImageProcessingScripts/Image to PDF/Images/image3.jpg
Binary file added
ImageProcessingScripts/Image to PDF/Images/output.pdf
Binary file not shown.
Binary file added
ImageProcessingScripts/Image to PDF/Images/step1.png
Binary file added
ImageProcessingScripts/Image to PDF/Images/step2.png
Binary file added
ImageProcessingScripts/Image to PDF/Images/step3.png
Binary file added
ImageProcessingScripts/Image to PDF/Images/step4.png
Binary file added
ImageProcessingScripts/Image to PDF/Images/step5.png
Binary file added
ImageProcessingScripts/Image to PDF/Images/step6.png
192 changes: 192 additions & 0 deletions
ImageProcessingScripts/Image to PDF/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| <div align="center"> | ||
|
|
||
| # Image to PDF Converter in Python | ||
|
|
||
| </div> | ||
|
|
||
| ## Example Folder Structure | ||
|
|
||
| ``` | ||
| ImageToPDFConverter/ | ||
| ├── Images/ | ||
| │ ├── image1.png | ||
| │ ├── image2.jpeg | ||
| │ ├── image3.jpg | ||
| │ ├── step1.png | ||
| │ ├── step2.png | ||
| │ ├── step3.png | ||
| │ ├── step4.png | ||
| │ ├── step5.png | ||
| │ ├── step6.png | ||
| │ └── output.pdf | ||
| ├── README.md | ||
| └── image_to_pdf.py | ||
| ``` | ||
|
|
||
| ## Aim | ||
|
|
||
| The main aim of this project is to provide a simple and user-friendly **GUI interface** that allows users to upload multiple images and convert them into a single **PDF file**. | ||
| It also shows where the file is saved and automatically opens the generated PDF. | ||
|
|
||
| --- | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Make sure you have the following installed: | ||
|
|
||
| - **Python 3.x** | ||
| - Required Python libraries: | ||
| ```bash | ||
| pip install pillow | ||
| ``` | ||
|
|
||
| * (Optional) A virtual environment for cleaner setup. | ||
|
|
||
| --- | ||
|
|
||
| ## Purpose | ||
|
|
||
| The purpose of this project is to simplify the process of combining multiple image files (JPG, JPEG, PNG) into one PDF document using a graphical user interface (GUI) — **no command line required**. | ||
|
|
||
| --- | ||
|
|
||
| ## Description | ||
|
|
||
| This project uses **Tkinter** for the GUI, **Pillow (PIL)** for image handling, and **webbrowser** to open the created PDF automatically. | ||
|
|
||
| ### Features | ||
|
|
||
| * Upload multiple image files easily. | ||
| * Convert all selected images into a single PDF. | ||
| * Choose custom file name and save location. | ||
| * Automatically open the generated PDF after saving. | ||
| * Modern and clean user interface. | ||
|
|
||
| --- | ||
|
|
||
| ## Libraries Used | ||
|
|
||
| * `tkinter` → for GUI | ||
| * `Pillow` → for image processing | ||
| * `webbrowser` → to open the created PDF file | ||
| * `os` → to handle file paths | ||
|
|
||
| --- | ||
|
|
||
| ## Workflow of the Project | ||
|
|
||
| ### 1. `upload_files()` function | ||
|
|
||
| This function opens a file dialog for selecting images (`.jpg`, `.jpeg`, `.png`) and stores them in a list. | ||
|
|
||
| ### 2. `convert_to_pdf()` function | ||
|
|
||
| * Converts all selected images to RGB format. | ||
| * Merges them into one PDF file. | ||
| * Saves the file to a user-selected location. | ||
| * Opens the PDF automatically using the system’s default viewer. | ||
|
|
||
| ### 3. GUI Layout | ||
|
|
||
| * A label that guides the user. | ||
| * Two buttons: | ||
|
|
||
| * **Upload Images** | ||
| * **Convert to PDF** | ||
|
|
||
| --- | ||
|
|
||
| ## Setup Instructions | ||
|
|
||
| 1. **Clone the repository or copy the script** | ||
|
|
||
| ```bash | ||
| git clone <your_repo_url> | ||
| ``` | ||
|
|
||
| Or simply download the `.py` file. | ||
|
|
||
| 2. **Navigate to the directory** | ||
|
|
||
| ```bash | ||
| cd ImageToPDFConverter | ||
| ``` | ||
|
|
||
| 3. **Create and activate a virtual environment (recommended)** | ||
|
|
||
| For Linux/Mac : | ||
| ```bash | ||
| python -m venv myenv | ||
| source myenv/bin/activate | ||
| ``` | ||
| For Windows: | ||
| ```bash | ||
| myenv\Scripts\activate | ||
| ``` | ||
|
|
||
| 4. **Install dependencies** | ||
|
|
||
| ```bash | ||
| pip install pillow | ||
| ``` | ||
|
|
||
| 5. **Run the program** | ||
|
|
||
| ```bash | ||
| python image_to_pdf.py | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Example Output | ||
|
|
||
| When you run the app: | ||
|
|
||
| 1. Click **" Upload Images"** and select your image files. | ||
| 2. Click **" Convert to PDF"**. | ||
| 3. Choose where to save your PDF. | ||
| 4. A success message appears and your PDF opens automatically! | ||
|
|
||
| --- | ||
|
|
||
| ## Example | ||
|
|
||
| If you upload the image: | ||
|
|
||
| ``` | ||
| image1.png | ||
| ``` | ||
|
|
||
| The app merges them into a PDF file like: | ||
|
|
||
| ``` | ||
| output.pdf | ||
| ``` | ||
|
|
||
| Then automatically opens it. | ||
|
|
||
| --- | ||
|
|
||
| ## Screenshot (Example) | ||
|
|
||
| Example: | ||
|
|
||
| ``` | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
| ``` | ||
|
|
||
| --- | ||
| ## Author | ||
| **Shashwat** | ||
|
|
||
| --- | ||
|
|
||
| ## Conclusion | ||
|
|
||
| This project demonstrates how Python’s Tkinter and Pillow can be combined to create a simple yet effective GUI tool. | ||
| With minimal dependencies, this app is lightweight, easy to use, and automates the task of combining images into PDFs with a single click. |
94 changes: 94 additions & 0 deletions
ImageProcessingScripts/Image to PDF/images_to_pdf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from tkinter import Tk, Button, Label, filedialog, messagebox | ||
| from PIL import Image | ||
| import os | ||
| import webbrowser | ||
|
|
||
|
|
||
| class ImageToPDFApp: | ||
| """A simple Interface to convert images into a PDF file.""" | ||
|
|
||
| def __init__(self, root): | ||
| self.root = root | ||
| self.root.title("Image to PDF Converter") | ||
| self.root.geometry("420x260") | ||
| self.root.config(bg="#f4f4f4") | ||
|
|
||
| self.files = [] | ||
|
|
||
| self.label = Label( | ||
| root, | ||
| text="Upload your images to convert to PDF", | ||
| bg="#f4f4f4", | ||
| font=("Arial", 12) | ||
| ) | ||
| self.label.pack(pady=15) | ||
|
|
||
| self.upload_btn = Button( | ||
| root, | ||
| text=" Upload Images", | ||
| command=self.upload_files, | ||
| bg="#4285F4", | ||
| fg="white", | ||
| width=25 | ||
| ) | ||
| self.upload_btn.pack(pady=10) | ||
|
|
||
| self.convert_btn = Button( | ||
| root, | ||
| text="Convert to PDF", | ||
| command=self.convert_to_pdf, | ||
| bg="#34A853", | ||
| fg="white", | ||
| width=25 | ||
| ) | ||
| self.convert_btn.pack(pady=10) | ||
|
|
||
| def upload_files(self): | ||
| """Open a file dialog to select image files.""" | ||
| self.files = filedialog.askopenfilenames( | ||
| title="Select Images", | ||
| filetypes=[("Image Files", "*.jpg *.jpeg *.png")] | ||
| ) | ||
|
|
||
| if self.files: | ||
| messagebox.showinfo("Files Selected", f"{len(self.files)} image(s) selected.") | ||
| else: | ||
| messagebox.showwarning("No File", "Please select at least one image.") | ||
|
|
||
| def convert_to_pdf(self): | ||
| """Convert selected images into a single PDF and open it.""" | ||
| if not self.files: | ||
| messagebox.showerror("Error", "No images selected.") | ||
| return | ||
|
|
||
| output_name = filedialog.asksaveasfilename( | ||
| defaultextension=".pdf", | ||
| filetypes=[("PDF Files", "*.pdf")], | ||
| title="Save PDF As" | ||
| ) | ||
|
|
||
| if output_name: | ||
| images = [Image.open(f).convert("RGB") for f in self.files] | ||
| images[0].save(output_name, save_all=True, append_images=images[1:]) | ||
|
|
||
| messagebox.showinfo( | ||
| "Success", | ||
| f" PDF saved successfully!\n\n Location:\n{output_name}" | ||
| ) | ||
|
|
||
| # Automatically open the PDF after saving | ||
| try: | ||
| webbrowser.open_new(rf"file://{os.path.abspath(output_name)}") | ||
| except Exception as e: | ||
| messagebox.showwarning( | ||
| "Open File", | ||
| f"PDF saved but couldn't open automatically.\nError: {e}" | ||
| ) | ||
| else: | ||
| messagebox.showwarning("Cancelled", "Save operation cancelled.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| root = Tk() | ||
| app = ImageToPDFApp(root) | ||
| root.mainloop() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.