build.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/python
  2. #--------------------------------------------------------------------------------------------------------
  3. # This python file is used for processing a complete book.
  4. # It assumes that there is a folder named 'input' that contains all input images
  5. # The output is generated into a folder called 'output'
  6. # A folder called 'temp' is also used for intermediate files (that can be cleaned later)
  7. # In the 'input'-folder, there should only be image-files ending with a jpg-file-extension
  8. # You also need a book_settings.py that store all book specific settings.
  9. #
  10. # To scan a book, start with all the right hand pages from the start to the end, then take all left hand pages
  11. # When starting over with the left hande page, its important to start with the same spread (page-pair) as the right-hand.
  12. # The number of right/left-pages must be equal
  13. #
  14. # The reference image and cover image can be taken in any order, but must be pointed out below in the 'arguments'
  15. #
  16. # Static Input arguments.
  17. ipath = './input'
  18. opath = './output'
  19. tpath = './temp'
  20. settingsFile = tpath + '/' + 'settings.txt'
  21. image_pre_name = 'IMG_' # The part of the filename before the numerical part
  22. image_post_name = '.JPG' # Part of the filename after the numerical part
  23. numberNumericalsInName=4 # Number of numericals in file name-index. For example 4 in 0304 and 1234
  24. # All the dynamic (book specific) settings are store in an external python-file.
  25. # This makes it easier to share this file while maintaining multiple settings-files.
  26. from book_settings import *
  27. #--------------------------------------------------------------------------------------------------------
  28. import os
  29. import subprocess
  30. import shutil
  31. # Do some clean-up first of folders
  32. shutil.rmtree(tpath, ignore_errors=True)
  33. shutil.rmtree(opath, ignore_errors=True)
  34. #subprocess.call('rm -rf ' + tpath)
  35. #subprocess.call('rm -rf ' + opath)
  36. # Create output folders, if they don't exist
  37. if not os.path.exists(opath): os.makedirs(opath)
  38. if not os.path.exists(tpath): os.makedirs(tpath)
  39. # Check that there is an input folder
  40. if not os.path.exists(ipath):
  41. print('ERROR: No input folder !')
  42. exit()
  43. # --------- Create the settings.txt-file (Used for imgage-cropping)
  44. file = open(settingsFile,'w')
  45. file.write('# Autogenerated file.\nSTART:\n' + str(cropX1) + '\n' + str(cropY1) + '\n' + str(cropX2) + '\n' + str(cropY2) + '\n')
  46. file.close()
  47. # --------- Loop through all images (*.jpg) in the input folder and sort them according to their name
  48. # The list contains complete full path filenames
  49. pics = sorted([os.path.join(ipath, pic)
  50. for pic in os.listdir(ipath)
  51. if pic.endswith(image_post_name) and os.path.isfile(os.path.join(ipath, pic))])
  52. # --------- Create the lists that will contain the left and right hand pages
  53. rHandSides = []
  54. lHandSides = []
  55. covFilePath = '' # Full path filename of the cover image
  56. refFilePath = '' # Full path filename of the reference image
  57. # --------- Loop through all images in "raw" image list and put them into two lists, one for right pages and one for left
  58. # When we get to the image with the number that matches firstLeftHandPage, then we switch to adding files to that left-list instead
  59. addFilesToRightList = True # If we should add images to the right-hand page list (at the beginning)
  60. for ifile in pics:
  61. imgNoStartPos = ifile.find(image_pre_name)+len(image_pre_name) # Get start pos of the number portion
  62. imgNumber = int( ifile[imgNoStartPos:imgNoStartPos+numberNumericalsInName] ) # Int containing the numberical part of the image-name
  63. # Check if it's the cover image or reference image
  64. if covFilePath == '' and imgNumber == coverImage:
  65. covFilePath = ifile
  66. print( 'Cover file:' + ifile )
  67. elif refFilePath == '' and imgNumber == refImg:
  68. refFilePath = ifile
  69. print( 'Reference file:' + ifile )
  70. else:
  71. if addFilesToRightList == True and imgNumber >= firstLeftHandPage:
  72. addFilesToRightList=False
  73. if addFilesToRightList == True:
  74. rHandSides.append( ifile )
  75. else:
  76. lHandSides.append( ifile )
  77. # ------ Check that we have found both ref-file and cover-file
  78. if refFilePath == '' or covFilePath == '':
  79. print('ERROR: Missing cover or reference image')
  80. exit()
  81. # ------ Now it's time to create the white and black reference files.
  82. # They are created in the root-folder of the project so we need to move them to temp-folder
  83. print( '---------- Creating reference files ----------' )
  84. subprocess.call('../RefImageCreator/build-Mac/RefImageCreator -i ' + refFilePath, shell=True)
  85. # Move ref-files to the temp-folder
  86. shutil.move('./ref_black.jpg', tpath )
  87. shutil.move('./ref_white.jpg', tpath )
  88. leftPageNo = pageNoOfFirstLeftPage # Start page-number for left and right output pages
  89. rightPageNo = pageNoOfFirstLeftPage+1
  90. print( '---------- Processing cover page ----------' )
  91. # ------ First convert the cover-page as it should be number one
  92. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  93. ' -i ' + covFilePath + \
  94. ' -o ' + opath + '/page_'+str(1).zfill(numberNumericalsInName)+'.bmp ' + \
  95. ' -wref ' + tpath + '/ref_white.jpg' + \
  96. ' -bref ' + tpath + '/ref_black.jpg' + \
  97. ' -s ' + settingsFile + \
  98. ' -r 270', shell=True)
  99. # ------ Start doing the rest of the pages. Left pages first
  100. for i in range( len(lHandSides) ):
  101. # Process it
  102. print( '---------- Processing left-hand page: ' + str(leftPageNo) + ' --- File: '+lHandSides[i]+'----------' )
  103. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  104. ' -q ' \
  105. ' -i ' + lHandSides[i] + \
  106. ' -o ' + opath + '/page_'+str(leftPageNo).zfill(numberNumericalsInName)+'.bmp ' + \
  107. ' -wref ' + tpath + '/ref_white.jpg' + \
  108. ' -bref ' + tpath + '/ref_black.jpg' + \
  109. ' -s ' + settingsFile + \
  110. ' -r 90', shell=True)
  111. leftPageNo = leftPageNo + 2 # Increase the page conter
  112. # ------ Continue with the rest of the right hand pages
  113. for i in range( len(rHandSides) ):
  114. # Right page after. Process it
  115. print( '---------- Processing right-hand page: ' + str(rightPageNo) + ' --- File: '+rHandSides[i]+'----------' )
  116. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  117. ' -q ' \
  118. ' -i ' + rHandSides[i] + \
  119. ' -o ' + opath + '/page_'+str(rightPageNo).zfill(numberNumericalsInName)+'.bmp ' + \
  120. ' -wref ' + tpath + '/ref_white.jpg' + \
  121. ' -bref ' + tpath + '/ref_black.jpg' + \
  122. ' -s ' + settingsFile + \
  123. ' -r 270', shell=True)
  124. rightPageNo = rightPageNo + 2 # Increase the page conter