Skip to content

Instantly share code, notes, and snippets.

@monperrus
Created March 16, 2025 10:07
Show Gist options
  • Select an option

  • Save monperrus/6e5a9c73dba907966292ee0b9783e56a to your computer and use it in GitHub Desktop.

Select an option

Save monperrus/6e5a9c73dba907966292ee0b9783e56a to your computer and use it in GitHub Desktop.
small python script to concatenate pictures as jpg files into a PDF of the same size
#!/usr/bin/python3
# small python script to concatenate pictures as jpg files into a PDF of the same size
# Nov 2024, chatgpt
# pip3 install reportlab
import os
from PIL import Image
from reportlab.pdfgen import canvas
import sys
import glob
def images_to_pdf(image_files, output_pdf):
# Create a PDF canvas
c = canvas.Canvas(output_pdf)
for image_file in image_files:
print(image_file)
# Open the image
img = Image.open(image_file)
# Get image dimensions
width, height = img.size
# Set the page size to the image size
c.setPageSize((width, height))
# Draw the image on the PDF
c.drawImage(image_file, 0, 0, width, height)
# End the page
c.showPage()
# Save the PDF
c.save()
if __name__ == "__main__":
# Specify the directory containing the images
image_directory = sys.argv[1]
# Get a list of JPG files in the directory
image_files = sorted(glob.glob(image_directory+'/*.jpg'))
# Sort the files if needed (optional)
image_files.sort()
# Specify the output PDF file name
output_pdf = "concatenated-pictures.pdf"
# Convert images to PDF
images_to_pdf(image_files, output_pdf)
print(f"PDF created successfully: {output_pdf}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment