Created
April 11, 2025 01:13
-
-
Save SColson82/ea0442a522c231788d1ce7e55fd657e7 to your computer and use it in GitHub Desktop.
Concatenates multiple image files into one contiguous pdf. Great for class notes.
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
| from PIL import Image | |
| import os | |
| def images_in_folder_to_pdf(input_folder="images", output_path="output.pdf"): | |
| """ | |
| Takes every image file in a directory called 'images' and concatenates them to | |
| one contiguous document. Great for creating one pdf of Professor Kubiak's notes. | |
| Suggestion: load into a Colab notebook, not pip installs necessary and takes less | |
| time to run than it does to upload the images. | |
| """ | |
| supported_formats = (".jpg", ".jpeg", ".png") | |
| image_files = [ | |
| os.path.join(input_folder, f) | |
| for f in sorted(os.listdir(input_folder)) | |
| if f.lower().endswith(supported_formats) | |
| ] | |
| if not image_files: | |
| print("No supported image files found in the folder.") | |
| return | |
| image_list = [Image.open(f).convert("RGB") for f in image_files] | |
| # Save the first image and append the rest | |
| image_list[0].save(output_path, save_all=True, append_images=image_list[1:]) | |
| print(f"PDF saved as '{output_path}' with {len(image_list)} pages.") | |
| # Run it | |
| images_in_folder_to_pdf("images", "output.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment