# This scripts is doing...
# Patrick Kuehl, date


import os
from matplotlib.backends.backend_pdf import PdfPages
import PIL

def merge_scanned_images(path,type,pdfname):
  os.system("mkdir temp")
  file_names = sorted((fn for fn in os.listdir(path) if fn.endswith('.%s'%type)))
  #print file_names

  for idx,file_name in enumerate(file_names):
    this_image=PIL.Image.open("%s/%s" %(path,file_name))
    this_image.save("temp/%03d.pdf"%(idx), "PDF", Quality = 100)
  os.system("pdftk temp/* cat output %s.pdf"%(pdfname))
  os.system("rm -r temp")

merge_scanned_images("folder_containing_images","jpg","output_pdfname")
