build.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 in the input folder and put them into two lists, one for right pages and one for left
  48. pics = sorted([os.path.join(ipath, pic)
  49. for pic in os.listdir(ipath)
  50. if pic.endswith(image_post_name) and os.path.isfile(os.path.join(ipath, pic))])
  51. rHandSides = []
  52. lHandSides = []
  53. covFile = image_pre_name + str(int(coverImage)).zfill(numberNumericalsInName) + image_post_name
  54. refFile = image_pre_name + str(int(refImg)).zfill(numberNumericalsInName) + image_post_name
  55. covFilePath = ''
  56. refFilePath = ''
  57. for ifile in pics:
  58. imgNoStartPos = ifile.find(image_pre_name)+len(image_pre_name)
  59. imgNumber = int( ifile[imgNoStartPos:imgNoStartPos+numberNumericalsInName] )
  60. # Check if it's the cover image or reference image
  61. if ifile.find( covFile ) > 0:
  62. covFilePath = ifile
  63. print( 'Cover file:' + ifile )
  64. elif ifile.find( refFile ) > 0:
  65. refFilePath = ifile
  66. print( 'Reference file:' + ifile )
  67. elif imgNumber < firstLeftHandPage:
  68. rHandSides.append( imgNumber )
  69. elif imgNumber >= firstLeftHandPage:
  70. lHandSides.append( imgNumber )
  71. if len(rHandSides) != len(lHandSides):
  72. print('Error: Number of left and right hand sides must be equal. Left:' + str(len(lHandSides)) + ' Right:'+ str(len(rHandSides)) )
  73. exit()
  74. if refFilePath == '' or covFilePath == '':
  75. print('ERROR: Missing cover or reference image')
  76. exit()
  77. print( '---------- Creating reference files ----------' )
  78. subprocess.call('../RefImageCreator/build-Mac/RefImageCreator -i ' + refFilePath, shell=True)
  79. # Move ref-files to the temp-folder
  80. shutil.move('./ref_black.jpg', tpath )
  81. shutil.move('./ref_white.jpg', tpath )
  82. pageNo = 1 # Page-number for output pages
  83. print( '---------- Processing cover page ----------' )
  84. # First convert the cover-page as it should be number one
  85. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  86. ' -i ' + covFilePath + \
  87. ' -o ' + tpath+'/temp.jpg' + \
  88. ' -wref ' + tpath + '/ref_white.jpg' + \
  89. ' -bref ' + tpath + '/ref_black.jpg' + \
  90. ' -s ' + settingsFile , shell=True)
  91. # Rotate the image
  92. subprocess.call('jpegtran -rotate 90 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
  93. # Increase the page conter
  94. pageNo = pageNo + 1
  95. # Start doing the rest of the pages. Left pages first and then the right page
  96. for i in range( len(lHandSides) ):
  97. # Left page first. Process it
  98. print( '---------- Processing left-hand page: ' + str(pageNo) + ' ----------' )
  99. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  100. ' -i ' + ipath + '/' + image_pre_name + str(lHandSides[i]).zfill(numberNumericalsInName) + image_post_name + \
  101. ' -o ' + tpath+'/temp.jpg' + \
  102. ' -wref ' + tpath + '/ref_white.jpg' + \
  103. ' -bref ' + tpath + '/ref_black.jpg' + \
  104. ' -s ' + settingsFile , shell=True)
  105. # Rotate the image
  106. subprocess.call('jpegtran -rotate 90 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
  107. pageNo = pageNo + 1 # Increase the page conter
  108. # Right page after. Process it
  109. print( '---------- Processing right-hand page: ' + str(pageNo) + ' ----------' )
  110. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  111. ' -i ' + ipath + '/' + image_pre_name + str(rHandSides[i]).zfill(numberNumericalsInName) + image_post_name + \
  112. ' -o ' + tpath+'/temp.jpg' + \
  113. ' -wref ' + tpath + '/ref_white.jpg' + \
  114. ' -bref ' + tpath + '/ref_black.jpg' + \
  115. ' -s ' + settingsFile , shell=True)
  116. # Rotate the image
  117. subprocess.call('jpegtran -rotate 270 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
  118. pageNo = pageNo + 1 # Increase the page conter