Просмотр исходного кода

Working left and right pages with page numbers. Done

Thomas Chef 7 лет назад
Родитель
Сommit
4838dc84f9

+ 3 - 1
PageNormalizer/main.cpp

@@ -23,8 +23,10 @@ int main(int argc, char *argv[])
     std::string file_settings( cimg_option("-s","./settings.txt","Settings file") );
     
     std::string file_o( cimg_option("-o","./output.jpg","Output file") );
+    
+    const bool quiet = cimg_option("-q",false,0);      // This is a hidden option
         
-    PageNormalizer pn = PageNormalizer(file_i, file_o, file_white_ref, file_black_ref, file_settings);
+    PageNormalizer pn = PageNormalizer(file_i, file_o, file_white_ref, file_black_ref, file_settings, quiet);
 
 
 

+ 6 - 3
PageNormalizer/pagenormalizer.cpp

@@ -4,10 +4,13 @@
 
 PageNormalizer::PageNormalizer() {
     width = height = 0;
+    m_quiet = false;
 }
 
-PageNormalizer::PageNormalizer(const std::string inFile,const std::string outFile,const std::string whiteRef,const std::string blackRef,const std::string settingsFile) {
+PageNormalizer::PageNormalizer(const std::string inFile,const std::string outFile,const std::string whiteRef,const std::string blackRef,const std::string settingsFile, const bool quiet ) {
 
+    m_quiet = quiet;
+    
     // Read settings from text-file
     ReadSettings( settingsFile.c_str() );
     
@@ -21,7 +24,7 @@ PageNormalizer::PageNormalizer(const std::string inFile,const std::string outFil
     height = inImage.height();
 
     
-    printf("Crop:%d,%d-%d,%d\n",cropX0,cropY0,cropX1,cropY1);
+    if( !m_quiet ) printf("Crop:%d,%d-%d,%d\n",cropX0,cropY0,cropX1,cropY1);
     
     outImage = CImg<>(cropX1-cropX0,cropY1-cropY0,1,3);
 
@@ -67,7 +70,7 @@ int PageNormalizer::GetBlackness(unsigned int x,unsigned int y) {
 
 void PageNormalizer::ReadSettings(const std::string inFile)
 {
-    std::cout << "Reading settings from: " << inFile << std::endl;
+    if( !m_quiet ) std::cout << "Reading settings from: " << inFile << std::endl;
     
     std::string line;
     std::ifstream myfile( inFile.c_str() );

+ 3 - 1
PageNormalizer/pagenormalizer.h

@@ -10,7 +10,7 @@ class PageNormalizer
 {
 public:
     PageNormalizer();
-    PageNormalizer(const std::string inFile,const std::string outFile,const std::string whiteRef,const std::string blackRef,const std::string settingsFile);
+    PageNormalizer(const std::string inFile,const std::string outFile,const std::string whiteRef,const std::string blackRef,const std::string settingsFile, const bool quiet );
 
 
 
@@ -33,6 +33,8 @@ private:
     int cropY0;
     int cropX1;
     int cropY1;
+    
+    bool m_quiet; 
 };
 
 #endif

+ 4 - 3
TestProject/book_settings.py

@@ -1,7 +1,8 @@
 # Dynamic Input arguments. These very for every book that is scanned and for every scan
-refImg =   3                    # The image used as reference image
-coverImage = 12               # Image used for cover (Will be placed 1st)
-firstLeftHandPage = 8         # The first image covering the left pages
+refImg =   2                    # The image used as reference image
+coverImage = 1                  # Image used for cover (Will be placed 1st)
+firstLeftHandPage = 212          # The first image covering the left pages
+pageNoOfFirstLeftPage = 2       # The page number (printed in the book) of the first left-hand page that is scanned (An even number)
 cropX1 = 333                    # Crop coordinates of all the images
 cropY1 = 30
 cropX2 = 3342

+ 44 - 35
TestProject/build.py

@@ -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