Skip to content

Instantly share code, notes, and snippets.

Combine JPG Images to PDF in Python
# Directory containing JPG images
image_directory = "D:\\Files\\images\\"
# Get a list of all JPG files in the directory
file_paths = sorted([os.path.join(image_directory, f) for f in os.listdir(image_directory) if f.startswith("sam") and f.endswith(".jpg")])
import os
import aspose.pdf as pdf
from aspose.pdf import Document, Image, Page
# Create a PDF document and add the image to it
doc = Document()
page = doc.pages.add()
# Add images to the page
for img in file_paths:
# Create an image object
image = Image()
# Set the image file stream
image.file = img
# Add the image into the paragraphs collection of the page
page.paragraphs.add(image)
# Save the document
doc.save("merged-jpgs-to-PDF.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment