Skip to content

Instantly share code, notes, and snippets.

@butlerblog
Created May 24, 2023 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save butlerblog/0b979127f157b13098352d64f20c26bd to your computer and use it in GitHub Desktop.
Save butlerblog/0b979127f157b13098352d64f20c26bd to your computer and use it in GitHub Desktop.
Python script to create a PDF from a directory of sequentially named images
import img2pdf
import argparse
import os
def process_images(min_range, max_range, img_path, prefix, suffix, out_file):
images = []
for i in range(min_range,max_range):
fname = img_path + prefix + '%0.3d' % i + '.' + suffix
print("adding img: " + fname)
images.append(fname)
out_file.write(img2pdf.convert(images))
img_path = input("Path to image files: ")
formatted_img_path = os.path.join(img_path, '')
prefix = input("Image file prefix: ")
suffix = input("Image file type: ")
out_fname = input("Save PDF as (filename.pdf): ")
min_range = 1
max_range = input("Number of pages (images): ")
max_range = int(max_range) + 1
with open(out_fname, "wb") as out_file:
process_images(min_range, max_range, formatted_img_path, prefix, suffix, out_file)
print("PDF generation complete!")
print("File saved as: " + out_fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment