Skip to content

Instantly share code, notes, and snippets.

@SColson82
Created April 11, 2025 01:13
Show Gist options
  • Save SColson82/ea0442a522c231788d1ce7e55fd657e7 to your computer and use it in GitHub Desktop.
Save SColson82/ea0442a522c231788d1ce7e55fd657e7 to your computer and use it in GitHub Desktop.

Revisions

  1. SColson82 created this gist Apr 11, 2025.
    30 changes: 30 additions & 0 deletions ConcatenateImages.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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")