build.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/python
  2. #--------------------------------------------------------------------------------------------------------
  3. # This python file is processing a complete book
  4. # It assumes that there is a folder named 'input' that contains all input data
  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. #
  9. # To scan a book, start with all the right hand pages from the start to the end, then take all left hand pages
  10. # The number of right/left-pages must be equal
  11. #
  12. # The reference image and cover image can be taken in any order, but must be pointed out below in the 'arguments'
  13. #
  14. # Static Input arguments.
  15. ipath = './input'
  16. opath = './output'
  17. tpath = './temp'
  18. settingsFile = tpath + '/' + 'settings.txt'
  19. image_pre_name = 'IMG_' # The part of the filename before the numerical part
  20. image_post_name = '.JPG' # Part of the filename after the numerical part
  21. numberNumericalsInName=4 # Number of numericals in file name-index. For example 4 in 0304 and 1234
  22. # Dynamic Input arguments. These very for every book that is scanned and for every scan
  23. refImg = 3 # The image used as reference image
  24. coverImage = 12 # Image used for cover (Will be placed 1st)
  25. firstLeftHandPage = 8 # The first image covering the left pages
  26. cropX1 = 333 # Crop coordinates of all the images
  27. cropY1 = 30
  28. cropX2 = 3342
  29. cropY2 = 2380
  30. #--------------------------------------------------------------------------------------------------------
  31. import os
  32. import subprocess
  33. import shutil
  34. # Do some clean-up first of folders
  35. shutil.rmtree(tpath, ignore_errors=True)
  36. shutil.rmtree(opath, ignore_errors=True)
  37. #subprocess.call('rm -rf ' + tpath)
  38. #subprocess.call('rm -rf ' + opath)
  39. # Create output folders, if they don't exist
  40. if not os.path.exists(opath): os.makedirs(opath)
  41. if not os.path.exists(tpath): os.makedirs(tpath)
  42. if not os.path.exists(ipath):
  43. print('ERROR: No input folder !')
  44. exit()
  45. # Create the settings.txt-file (Used for imgage-cropping)
  46. file = open(settingsFile,'w')
  47. file.write('# Autogenerated file.\nSTART:\n' + str(cropX1) + '\n' + str(cropY1) + '\n' + str(cropX2) + '\n' + str(cropY2) + '\n')
  48. file.close()
  49. # Loop through all images in the input folder and put them into two lists, one for right pages and one for left
  50. pics = sorted([os.path.join(ipath, pic)
  51. for pic in os.listdir(ipath)
  52. if pic.endswith(image_post_name) and os.path.isfile(os.path.join(ipath, pic))])
  53. rHandSides = []
  54. lHandSides = []
  55. covFile = image_pre_name + str(int(coverImage)).zfill(numberNumericalsInName) + image_post_name
  56. refFile = image_pre_name + str(int(refImg)).zfill(numberNumericalsInName) + image_post_name
  57. covFilePath = ''
  58. refFilePath = ''
  59. for ifile in pics:
  60. imgNoStartPos = ifile.find(image_pre_name)+len(image_pre_name)
  61. imgNumber = int( ifile[imgNoStartPos:imgNoStartPos+numberNumericalsInName] )
  62. # Check if it's the cover image or reference image
  63. if ifile.find( covFile ) > 0:
  64. covFilePath = ifile
  65. print( 'Cover file:' + ifile )
  66. elif ifile.find( refFile ) > 0:
  67. refFilePath = ifile
  68. print( 'Reference file:' + ifile )
  69. elif imgNumber < firstLeftHandPage:
  70. rHandSides.append( imgNumber )
  71. elif imgNumber >= firstLeftHandPage:
  72. lHandSides.append( imgNumber )
  73. if len(rHandSides) != len(lHandSides):
  74. print('Error: Number of left and right hand sides must be equal. Left:' + str(len(lHandSides)) + ' Right:'+ str(len(rHandSides)) )
  75. exit()
  76. if refFilePath == '' or covFilePath == '':
  77. print('ERROR: Missing cover or reference image')
  78. exit()
  79. print( '---------- Creating reference files ----------' )
  80. subprocess.call('../RefImageCreator/build-Mac/RefImageCreator -i ' + refFilePath, shell=True)
  81. # Move ref-files to the temp-folder
  82. shutil.move('./ref_black.jpg', tpath )
  83. shutil.move('./ref_white.jpg', tpath )
  84. pageNo = 1 # Page-number for output pages
  85. print( '---------- Processing cover page ----------' )
  86. # First convert the cover-page as it should be number one
  87. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  88. ' -i ' + covFilePath + \
  89. ' -o ' + tpath+'/temp.jpg' + \
  90. ' -wref ' + tpath + '/ref_white.jpg' + \
  91. ' -bref ' + tpath + '/ref_black.jpg' + \
  92. ' -s ' + settingsFile , shell=True)
  93. # Rotate the image
  94. subprocess.call('jpegtran -rotate 90 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
  95. # Increase the page conter
  96. pageNo = pageNo + 1
  97. # Start doing the rest of the pages. Left pages first and then the right page
  98. for i in range( len(lHandSides) ):
  99. # Left page first. Process it
  100. print( '---------- Processing left-hand page: ' + str(pageNo) + ' ----------' )
  101. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  102. ' -i ' + ipath + '/' + image_pre_name + str(lHandSides[i]).zfill(numberNumericalsInName) + image_post_name + \
  103. ' -o ' + tpath+'/temp.jpg' + \
  104. ' -wref ' + tpath + '/ref_white.jpg' + \
  105. ' -bref ' + tpath + '/ref_black.jpg' + \
  106. ' -s ' + settingsFile , shell=True)
  107. # Rotate the image
  108. subprocess.call('jpegtran -rotate 90 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
  109. pageNo = pageNo + 1 # Increase the page conter
  110. # Right page after. Process it
  111. print( '---------- Processing right-hand page: ' + str(pageNo) + ' ----------' )
  112. subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
  113. ' -i ' + ipath + '/' + image_pre_name + str(rHandSides[i]).zfill(numberNumericalsInName) + image_post_name + \
  114. ' -o ' + tpath+'/temp.jpg' + \
  115. ' -wref ' + tpath + '/ref_white.jpg' + \
  116. ' -bref ' + tpath + '/ref_black.jpg' + \
  117. ' -s ' + settingsFile , shell=True)
  118. # Rotate the image
  119. subprocess.call('jpegtran -rotate 270 -outfile ' + opath + '/page_'+str(pageNo).zfill(numberNumericalsInName)+'.jpg ' + tpath+'/temp.jpg', shell=True)
  120. pageNo = pageNo + 1 # Increase the page conter