Pdf made up of pictures

I had to embed 50 pictures in a pdf file for print - don't ask me why. Dependencies (on Debian):

  1. apt-get install imagemagick
  2. apt-get install pdftk

This is the Python script. It uses commands "montage" (from imagemagick) and pdftk:

  1. from subprocess import call
  2. import os
  3.  
  4. folder = 'pictures'
  5. images_per_page=4
  6. columns=2
  7.  
  8. print "Work in progress..."
  9.  
  10. files = os.listdir(folder)
  11. files.sort()
  12. pages = [files[i:i+images_per_page] for i in range(0, len(files), images_per_page)]
  13. pages_names = []
  14. for i in range(0, len(pages)):
  15. montage = ['montage']
  16. print "generating page " + str(i)
  17. files_in_page = [folder + os.sep + j for j in pages[i]]
  18. montage.extend(files_in_page)
  19. montage.extend(['-mode', 'Concatenate', '-tile', str(columns) + 'x'])
  20. output_page = folder + os.sep + 'page' + str(i) + '.pdf'
  21. pages_names.append(output_page)
  22. montage.append(output_page)
  23. call(montage)
  24.  
  25. pdftk = ['pdftk']
  26. pdftk.extend(pages_names)
  27. pdftk.extend(['cat', 'output', 'output.pdf'])
  28. call(pdftk)
  29.  
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.