Combine to Page Spreads

Books
Purpose:

This program takes a directory of individual book scans and combines them into page spreads. Page spreads present the data in the book's intended page spread format.

Dependencies

Python (with os module), ImageMagick

#REQUIRES IMAGEMAGIC ON MACHINE
import os

directory = "#DIRECTORY_OF_PHOTOS#"
data_type = "#DATA_TYPE#"                #The type of images you are combining (scrapbook, yearbook, etc.)
year = "#YEAR#"                         #This will be included in every filename and helps identitify the exact data source - if this is a yearbook, include the yearbook date or number


# Sorts images in the directory to ensure they are combined in the right order.
files = os.listdir(directory)
def getint(name):
   if year in str(name) and "COVER" not in str(name) and "BACK" not in str(name):
       if "start" in str(name):
           num_start = str(name).find("_") + len("_")
           temp_num = name[num_start:]
           num_end = temp_num.find("_")
           num = (temp_num[:num_end])
       else:
           num_start = str(name).find("_") + len("_")
           temp_num = name[num_start:]
           num_end = temp_num.find(".")
           num = (temp_num[:num_end])
   else:
       num = 0
   return int(num)


files = sorted(files, key=getint)

# Combines the images into page spreads with descriptive filenames. This might take some time to render.
def combine (left, right, page_count_1, page_count_2):
   print(left, right, page_count_1, page_count_2)
   bashCommand = "convert +append " + str(left) + " " + str(right) + " " + str(year) + "_"+ data_type + "_" + str(page_count_1) + "+" + str(page_count_2) + ".jpeg"
   os.system(bashCommand)
   print("combined")
   return


# The program starts here. It iterates though each file in the directory (except for the COVER and BACK) and runs the combine function every two pages.
combine_count = 0. )
page_count_1 = 0
page_count_2 = 0
left = ""
right = ""
start = False
for file in files:
   try:
       if ".jpeg" in str(file) and "COVER" not in str(file) and "BACK" not in str(file):
           if "start" in str(file):
               page_count_1 = 0
               page_count_2 = 0
               start = True
           if start != True:
               file_path = directory + "/" + str(file)
               combine_count += 1
               if combine_count == 1:
                   left = str(file_path)
                   page_count_1 -= 1
                   page_count_2 -= 1
               elif combine_count == 2:
                   combine_count = 0
                   right = str(file_path)
                   page_count_2 -= 1
                   combine(left, right, page_count_1, page_count_2)
                   page_count_1 -= 1
           if start == True:
               file_path = directory + "/" + str(file)
               combine_count += 1
               if combine_count == 1:
                   left = str(file_path)
                   page_count_1 += 1
                   page_count_2 += 1
               elif combine_count == 2:
                   combine_count = 0
                   right = str(file_path)
                   page_count_2 += 1
                   combine(left, right, page_count_1, page_count_2)
                   page_count_1 += 1
       else:
           continue
   except:
       print(file_path)
       print('\33[101m' + "*****ERROR*****" + '\x1b[0m')