import Image
import os
import re

#Tokeniser function
digits = re.compile(r'(\d+)')
def tokenize(filename):
    return tuple(int(token) if match else token
                 for token, match in
                 ((fragment, digits.search(fragment))
                  for fragment in digits.split(filename)))


root = ''
directory = root + ''
extension = '.jpg'
frontfirst = True
reqrot = 90
# A new directory to put merged images into
resdir = directory + '/new/'
#Create directory if not there
if not os.path.exists(resdir):
    os.makedirs(resdir)

#Sort the file list
files = [file for file in os.listdir(directory) if file.lower().endswith(extension)]
files.sort(key=tokenize)
#Create the arrays
if frontfirst:
    cardfronts = files[::2]
    cardbacks = files[1::2]
else:
    cardfronts = files[1::2]
    cardbacks = files[::2]

#Loop through both arrays
for f, b in zip(cardfronts, cardbacks):
    print(f, b)
    imf = Image.open(os.path.join(directory, f))
    imb = Image.open(os.path.join(directory, b))
    #rotate the images
    imf = imf.rotate(reqrot)
    imb = imb.rotate(reqrot)
    
    maxwd = max(imf.size[0],(imb.size[0]))
    sumht = imf.size[1] + imb.size[1]
    imnew = Image.new('RGB', (maxwd,sumht), 'black')
    #Paste them back together
    imnew.paste(imf, (0,0))
    imnew.paste(imb, (0,imf.size[1]))
    
    var1, var2 = f.split(".")
    var3, var4 = b.split(".")
    
    imnew.save(resdir + var1 + '_' + var3 + '_fb' + extension)

@@ -356,25 +356,104 @@
 pg'%0A
-frontfirst = True
+#Are the folders arranged with front image as odd numbers?%0Afrontfirst = True%0A#Rotation required 
 %0Areq
@@ -974,24 +974,8 @@
 s):%0A
-    print(f, b)%0A
     
@@ -1150,24 +1150,52 @@
 eqrot)%0A    %0A
+    #Work out maximum width%0A
     maxwd = 
@@ -1221,24 +1221,50 @@
 b.size%5B0%5D))%0A
+    #Add heights together%0A
     sumht = 
@@ -1289,16 +1289,38 @@
 size%5B1%5D%0A
+    #Create new image%0A
     imne
@@ -1460,24 +1460,44 @@
 ze%5B1%5D))%0A    
+#Split file names up
 %0A    var1, v
@@ -1545,24 +1545,39 @@
 it(%22.%22)%0A    
+#Save new image
 %0A    imnew.s

Update imageStitch.py