|
@@ -47,54 +47,61 @@ if not os.path.exists(ipath):
|
|
|
print('ERROR: No input folder !')
|
|
|
exit()
|
|
|
|
|
|
-# Create the settings.txt-file (Used for imgage-cropping)
|
|
|
+# --------- Create the settings.txt-file (Used for imgage-cropping)
|
|
|
file = open(settingsFile,'w')
|
|
|
file.write('# Autogenerated file.\nSTART:\n' + str(cropX1) + '\n' + str(cropY1) + '\n' + str(cropX2) + '\n' + str(cropY2) + '\n')
|
|
|
file.close()
|
|
|
|
|
|
-# Loop through all images in the input folder and put them into two lists, one for right pages and one for left
|
|
|
+
|
|
|
+# --------- Loop through all images (*.jpg) in the input folder and sort them according to their name
|
|
|
+# The list contains complete full path filenames
|
|
|
pics = sorted([os.path.join(ipath, pic)
|
|
|
for pic in os.listdir(ipath)
|
|
|
if pic.endswith(image_post_name) and os.path.isfile(os.path.join(ipath, pic))])
|
|
|
|
|
|
+# --------- Create the lists that will contain the left and right hand pages
|
|
|
rHandSides = []
|
|
|
lHandSides = []
|
|
|
-covFile = image_pre_name + str(int(coverImage)).zfill(numberNumericalsInName) + image_post_name
|
|
|
-refFile = image_pre_name + str(int(refImg)).zfill(numberNumericalsInName) + image_post_name
|
|
|
-covFilePath = ''
|
|
|
-refFilePath = ''
|
|
|
+covFilePath = '' # Full path filename of the cover image
|
|
|
+refFilePath = '' # Full path filename of the reference image
|
|
|
|
|
|
+# --------- Loop through all images in "raw" image list and put them into two lists, one for right pages and one for left
|
|
|
+# When we get to the image with the number that matches firstLeftHandPage, then we switch to adding files to that left-list instead
|
|
|
+addFilesToRightList = True # If we should add images to the right-hand page list (at the beginning)
|
|
|
for ifile in pics:
|
|
|
- imgNoStartPos = ifile.find(image_pre_name)+len(image_pre_name)
|
|
|
- imgNumber = int( ifile[imgNoStartPos:imgNoStartPos+numberNumericalsInName] )
|
|
|
+ imgNoStartPos = ifile.find(image_pre_name)+len(image_pre_name) # Get start pos of the number portion
|
|
|
+ imgNumber = int( ifile[imgNoStartPos:imgNoStartPos+numberNumericalsInName] ) # Int containing the numberical part of the image-name
|
|
|
# Check if it's the cover image or reference image
|
|
|
- if ifile.find( covFile ) > 0:
|
|
|
+ if covFilePath == '' and imgNumber == coverImage:
|
|
|
covFilePath = ifile
|
|
|
print( 'Cover file:' + ifile )
|
|
|
- elif ifile.find( refFile ) > 0:
|
|
|
+ elif refFilePath == '' and imgNumber == refImg:
|
|
|
refFilePath = ifile
|
|
|
print( 'Reference file:' + ifile )
|
|
|
- elif imgNumber < firstLeftHandPage:
|
|
|
- rHandSides.append( imgNumber )
|
|
|
- elif imgNumber >= firstLeftHandPage:
|
|
|
- lHandSides.append( imgNumber )
|
|
|
+ else:
|
|
|
+ if addFilesToRightList == True and imgNumber >= firstLeftHandPage:
|
|
|
+ addFilesToRightList=False
|
|
|
+
|
|
|
+ if addFilesToRightList == True:
|
|
|
+ rHandSides.append( ifile )
|
|
|
+ else:
|
|
|
+ lHandSides.append( ifile )
|
|
|
|
|
|
-if len(rHandSides) != len(lHandSides):
|
|
|
- print('Error: Number of left and right hand sides must be equal. Left:' + str(len(lHandSides)) + ' Right:'+ str(len(rHandSides)) )
|
|
|
- exit()
|
|
|
-
|
|
|
+# ------ Check that we have found both ref-file and cover-file
|
|
|
if refFilePath == '' or covFilePath == '':
|
|
|
print('ERROR: Missing cover or reference image')
|
|
|
exit()
|
|
|
|
|
|
+# ------ Now it's time to create the white and black reference files.
|
|
|
+# They are created in the root-folder of the project so we need to move them to temp-folder
|
|
|
print( '---------- Creating reference files ----------' )
|
|
|
subprocess.call('../RefImageCreator/build-Mac/RefImageCreator -i ' + refFilePath, shell=True)
|
|
|
-
|
|
|
# Move ref-files to the temp-folder
|
|
|
shutil.move('./ref_black.jpg', tpath )
|
|
|
shutil.move('./ref_white.jpg', tpath )
|
|
|
|
|
|
-pageNo = 1 # Page-number for output pages
|
|
|
+leftPageNo = pageNoOfFirstLeftPage # Start page-number for left and right output pages
|
|
|
+rightPageNo = pageNoOfFirstLeftPage+1
|
|
|
|
|
|
print( '---------- Processing cover page ----------' )
|
|
|
|
|
@@ -106,42 +113,44 @@ subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
|
|
|
' -bref ' + tpath + '/ref_black.jpg' + \
|
|
|
' -s ' + settingsFile , shell=True)
|
|
|
|
|
|
-# Rotate the image
|
|
|
-subprocess.call('jpegtran -rotate 90 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
|
|
|
+# Rotate the image and store it in the final position (output-folder)
|
|
|
+subprocess.call('jpegtran -rotate 270 -outfile ' + opath + '/page_'+str(1).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
|
|
|
|
|
|
-# Increase the page conter
|
|
|
-pageNo = pageNo + 1
|
|
|
|
|
|
-# Start doing the rest of the pages. Left pages first and then the right page
|
|
|
+# Start doing the rest of the pages. Left pages first
|
|
|
for i in range( len(lHandSides) ):
|
|
|
|
|
|
- # Left page first. Process it
|
|
|
- print( '---------- Processing left-hand page: ' + str(pageNo) + ' ----------' )
|
|
|
+ # Process it
|
|
|
+ print( '---------- Processing left-hand page: ' + str(leftPageNo) + ' --- File: '+lHandSides[i]+'----------' )
|
|
|
subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
|
|
|
- ' -i ' + ipath + '/' + image_pre_name + str(lHandSides[i]).zfill(numberNumericalsInName) + image_post_name + \
|
|
|
+ ' -q ' \
|
|
|
+ ' -i ' + lHandSides[i] + \
|
|
|
' -o ' + tpath+'/temp.jpg' + \
|
|
|
' -wref ' + tpath + '/ref_white.jpg' + \
|
|
|
' -bref ' + tpath + '/ref_black.jpg' + \
|
|
|
' -s ' + settingsFile , shell=True)
|
|
|
|
|
|
# Rotate the image
|
|
|
- subprocess.call('jpegtran -rotate 90 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
|
|
|
+ subprocess.call('jpegtran -rotate 90 -outfile ' + opath + '/page_'+str(leftPageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
|
|
|
+
|
|
|
|
|
|
+ leftPageNo = leftPageNo + 2 # Increase the page conter
|
|
|
|
|
|
- pageNo = pageNo + 1 # Increase the page conter
|
|
|
+# Continue with the rest of the right hand pages
|
|
|
+for i in range( len(rHandSides) ):
|
|
|
|
|
|
# Right page after. Process it
|
|
|
- print( '---------- Processing right-hand page: ' + str(pageNo) + ' ----------' )
|
|
|
+ print( '---------- Processing right-hand page: ' + str(rightPageNo) + ' --- File: '+rHandSides[i]+'----------' )
|
|
|
subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
|
|
|
- ' -i ' + ipath + '/' + image_pre_name + str(rHandSides[i]).zfill(numberNumericalsInName) + image_post_name + \
|
|
|
+ ' -q ' \
|
|
|
+ ' -i ' + rHandSides[i] + \
|
|
|
' -o ' + tpath+'/temp.jpg' + \
|
|
|
' -wref ' + tpath + '/ref_white.jpg' + \
|
|
|
' -bref ' + tpath + '/ref_black.jpg' + \
|
|
|
' -s ' + settingsFile , shell=True)
|
|
|
|
|
|
# Rotate the image
|
|
|
- subprocess.call('jpegtran -rotate 270 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
|
|
|
-
|
|
|
- pageNo = pageNo + 1 # Increase the page conter
|
|
|
+ subprocess.call('jpegtran -rotate 270 -outfile ' + opath + '/page_'+str(rightPageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
|
|
|
|
|
|
+ rightPageNo = rightPageNo + 2 # Increase the page conter
|
|
|
|