I had to embed 50 pictures in a pdf file for print – don’t ask me why. Dependencies (on Debian):
apt-get install imagemagick
apt-get install pdftk
This is the Python script. It uses commands “montage” (from imagemagick) and pdftk:
from subprocess import call
import os
folder = 'pictures'
images_per_page=4
columns=2
print "Work in progress..."
files = os.listdir(folder)
files.sort()
pages = [files[i:i+images_per_page] for i in range(0, len(files), images_per_page)]
pages_names = []
for i in range(0, len(pages)):
montage = ['montage']
print "generating page " + str(i)
files_in_page = [folder + os.sep + j for j in pages[i]]
montage.extend(files_in_page)
montage.extend(['-mode', 'Concatenate', '-tile', str(columns) + 'x'])
output_page = folder + os.sep + 'page' + str(i) + '.pdf'
pages_names.append(output_page)
montage.append(output_page)
call(montage)
pdftk = ['pdftk']
pdftk.extend(pages_names)
pdftk.extend(['cat', 'output', 'output.pdf'])
call(pdftk)