Skip to content

Instantly share code, notes, and snippets.

@lukasgabriel
Created October 12, 2022 06:32
Show Gist options
  • Save lukasgabriel/0057ee4bd175de387d5a96f66a5cc017 to your computer and use it in GitHub Desktop.
Save lukasgabriel/0057ee4bd175de387d5a96f66a5cc017 to your computer and use it in GitHub Desktop.
Convert nested images to PDF
import img2pdf
import pathlib
import os
ROOT_FOLDER = pathlib.WindowsPath("C:/Users/lukas/Desktop/sample/")
DEST_FOLDER = "C:/Users/lukas/Desktop/artbooks/"
FORMATS = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
# This is ugly code I'm perfectly aware of that 😶
for first_child_folder in ROOT_FOLDER.iterdir():
images = []
if first_child_folder.suffix:
continue
for second_child_folder in first_child_folder.iterdir():
for image in second_child_folder.iterdir():
if image.suffix in FORMATS:
#print(first_child_folder.__str__() + "->" + second_child_folder.__str__() + "->" + image.__str__())
images.append(image.__str__())
with open(f"{first_child_folder.__str__()}.pdf", "wb") as f:
f.write(img2pdf.convert(images, rotation=img2pdf.Rotation.ifvalid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment