Created
October 12, 2022 06:32
-
-
Save lukasgabriel/0057ee4bd175de387d5a96f66a5cc017 to your computer and use it in GitHub Desktop.
Convert nested images to PDF
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
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