Browse Source

First working version

Thomas Chef 7 years ago
commit
14799dc44a
37 changed files with 122762 additions and 0 deletions
  1. 5 0
      .gitignore
  2. 61029 0
      PageNormalizer/CImg.h
  3. 21 0
      PageNormalizer/PageNormalizer.pro
  4. 4 0
      PageNormalizer/build-Debug/.gitignore
  5. 34 0
      PageNormalizer/build-Mac/makefile
  6. 4 0
      PageNormalizer/build-Release/.gitignore
  7. BIN
      PageNormalizer/example/input_example.jpg
  8. BIN
      PageNormalizer/example/input_example2.jpg
  9. BIN
      PageNormalizer/example/ref_black.jpg
  10. BIN
      PageNormalizer/example/ref_white.jpg
  11. 16 0
      PageNormalizer/example/settings.txt
  12. BIN
      PageNormalizer/example/test1.JPG
  13. BIN
      PageNormalizer/example/test2.JPG
  14. 35 0
      PageNormalizer/main.cpp
  15. 91 0
      PageNormalizer/pagenormalizer.cpp
  16. 38 0
      PageNormalizer/pagenormalizer.h
  17. 61029 0
      RefImageCreator/CImg.h
  18. 21 0
      RefImageCreator/RefImageCreator.pro
  19. 4 0
      RefImageCreator/build-Debug/.gitignore
  20. 34 0
      RefImageCreator/build-Mac/makefile
  21. 4 0
      RefImageCreator/build-Release/.gitignore
  22. BIN
      RefImageCreator/example_imgs/ref_A.JPG
  23. BIN
      RefImageCreator/example_imgs/ref_B.JPG
  24. BIN
      RefImageCreator/example_imgs/ref_OLD.jpg
  25. 167 0
      RefImageCreator/imgmanipbs.cpp
  26. 43 0
      RefImageCreator/imgmanipbs.h
  27. 30 0
      RefImageCreator/main.cpp
  28. 4 0
      RefImageCreator/output/.gitignore
  29. 149 0
      TestProject/build.py
  30. BIN
      TestProject/input/img_0100.jpg
  31. BIN
      TestProject/input/img_0234.jpg
  32. BIN
      TestProject/input/img_0235.jpg
  33. BIN
      TestProject/input/img_0236.jpg
  34. BIN
      TestProject/input/img_0237.jpg
  35. BIN
      TestProject/input/img_0238.jpg
  36. BIN
      TestProject/input/img_0239.jpg
  37. BIN
      TestProject/input/img_0240.jpg

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+*~
+*.user
+*.o
+TestProject/temp
+TestProject/output

File diff suppressed because it is too large
+ 61029 - 0
PageNormalizer/CImg.h


+ 21 - 0
PageNormalizer/PageNormalizer.pro

@@ -0,0 +1,21 @@
+TEMPLATE = app
+CONFIG += console c++11
+CONFIG -= app_bundle
+CONFIG -= qt
+
+QMAKE_CXXFLAGS_RELEASE -= -O
+QMAKE_CXXFLAGS_RELEASE -= -O1
+QMAKE_CXXFLAGS_RELEASE -= -O2
+QMAKE_CXXFLAGS_RELEASE -= -O3
+QMAKE_CXXFLAGS_RELEASE += -O2
+
+QMAKE_LIBDIR += /usr/X11R6/lib
+
+SOURCES += main.cpp \
+    pagenormalizer.cpp
+
+HEADERS += \
+    CImg.h \
+    pagenormalizer.h
+
+LIBS += -lm -lpthread -lX11

+ 4 - 0
PageNormalizer/build-Debug/.gitignore

@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore

+ 34 - 0
PageNormalizer/build-Mac/makefile

@@ -0,0 +1,34 @@
+# Folders
+SRCDIR := ..
+BUILDDIR := .
+TARGETDIR := .
+
+CC := clang++ -arch x86_64
+
+# Targets
+EXECUTABLE := PageNormalizer
+TARGET := ./$(EXECUTABLE)
+
+# Code Lists
+SRCEXT := cpp
+SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
+OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
+
+# Shared Compiler Flags
+CFLAGS := -c -Dcimg_display=0
+INC := -I..
+LIB := -lm -ljpeg
+
+INCDIRS := 
+INCLIST := 
+BUILDLIST := 
+
+CFLAGS += -std=c++11 -stdlib=libc++ -O2
+
+$(TARGET): $(OBJECTS)
+	@echo "Linking..."
+	@echo "  Linking $(TARGET)"; $(CC) $^ -o $(TARGET) $(LIB)
+
+$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
+	@echo "Compiling $<..."; $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+

+ 4 - 0
PageNormalizer/build-Release/.gitignore

@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore

BIN
PageNormalizer/example/input_example.jpg


BIN
PageNormalizer/example/input_example2.jpg


BIN
PageNormalizer/example/ref_black.jpg


BIN
PageNormalizer/example/ref_white.jpg


+ 16 - 0
PageNormalizer/example/settings.txt

@@ -0,0 +1,16 @@
+This file contains settings for the PageNormalizer software
+Row 1 starts the line under "START:"
+Row and setting:
+1: x0 of Crop settings
+2: y0
+3: x1
+4: y1
+
+START:
+291
+60
+3685
+2711
+
+
+

BIN
PageNormalizer/example/test1.JPG


BIN
PageNormalizer/example/test2.JPG


+ 35 - 0
PageNormalizer/main.cpp

@@ -0,0 +1,35 @@
+#include <iostream>
+
+#include "CImg.h"
+using namespace cimg_library;
+
+#include "pagenormalizer.h"
+
+using namespace std;
+
+
+// Should be compiled with (in Linux)
+// g++ -o hello_word.exe hello_world.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11
+
+int getBrightness(const CImg<unsigned int>& image,unsigned int x,unsigned int y);
+
+int main(int argc, char *argv[])
+{
+    std::string file_i( cimg_option("-i","./input_example.jpg","Input image") );
+    
+    std::string file_white_ref( cimg_option("-wref","./ref_white.jpg","White ref image") );
+    std::string file_black_ref( cimg_option("-bref","./ref_black.jpg","Black ref image") );
+    
+    std::string file_settings( cimg_option("-s","./settings.txt","Settings file") );
+    
+    std::string file_o( cimg_option("-o","./output.jpg","Output file") );
+        
+    PageNormalizer pn = PageNormalizer(file_i, file_o, file_white_ref, file_black_ref, file_settings);
+
+
+
+    return 0;
+}
+
+
+

+ 91 - 0
PageNormalizer/pagenormalizer.cpp

@@ -0,0 +1,91 @@
+#include "pagenormalizer.h"
+#include <iostream>
+#include <fstream>
+
+PageNormalizer::PageNormalizer() {
+    width = height = 0;
+}
+
+PageNormalizer::PageNormalizer(const std::string inFile,const std::string outFile,const std::string whiteRef,const std::string blackRef,const std::string settingsFile) {
+
+    // Read settings from text-file
+    ReadSettings( settingsFile.c_str() );
+    
+    inImage = CImg<>( inFile.c_str() );      // Input image (Not manipulated)
+
+    refImages[0] = CImg<>( whiteRef.c_str() );    // White
+    refImages[1] = CImg<>( blackRef.c_str() );    // Black
+    
+
+    width = inImage.width();
+    height = inImage.height();
+
+    
+    printf("Crop:%d,%d-%d,%d\n",cropX0,cropY0,cropX1,cropY1);
+    
+    outImage = CImg<>(cropX1-cropX0,cropY1-cropY0,1,3);
+
+    NormalizeImage();
+
+    outImage.save_jpeg( outFile.c_str() );
+}
+
+void PageNormalizer::NormalizeImage()
+{
+
+    for(int y=cropY0;y<cropY1;y++)
+    {
+        for(int x=cropX0; x<cropX1;x++) {
+
+            int wh = GetWhiteness(x,y);
+            int bh = GetBlackness(x,y);
+
+            double whiteFactor = 255.0 / ( wh - bh );
+
+            int ch0 = ( inImage(x,y,0,0) - bh ) * whiteFactor;
+            int ch1 = ( inImage(x,y,0,1) - bh ) * whiteFactor;
+            int ch2 = ( inImage(x,y,0,2) - bh ) * whiteFactor;
+
+            if( ch0 < 0 ) ch0=0; if( ch1 < 0 ) ch1=0; if( ch2 < 0 ) ch2=0;
+            if( ch0 > 255 ) ch0=255; if( ch1 > 255 ) ch1=255; if( ch2 > 255 ) ch2=255;
+
+            outImage(x-cropX0,y-cropY0,0,0) = (unsigned char) ch0;
+            outImage(x-cropX0,y-cropY0,0,1) = (unsigned char) ch1;
+            outImage(x-cropX0,y-cropY0,0,2) = (unsigned char) ch2;
+
+        }
+    }
+
+}
+
+int PageNormalizer::GetWhiteness(unsigned int x,unsigned int y) {
+    return( refImages[0](x,y,0,0) );
+}
+int PageNormalizer::GetBlackness(unsigned int x,unsigned int y) {
+    return( refImages[1](x,y,0,0) );
+}
+
+void PageNormalizer::ReadSettings(const std::string inFile)
+{
+    std::cout << "Reading settings from: " << inFile << std::endl;
+    
+    std::string line;
+    std::ifstream myfile( inFile.c_str() );
+    if (myfile.is_open())
+    {
+        auto rowNum = -1;
+        while( std::getline( myfile,line ) )
+        {
+            switch( rowNum ) {
+                case -1: if( line.find("START:") == 0 ) rowNum = 0; break;
+                case 1: cropX0 = std::stoi(line);break;
+                case 2: cropY0 = std::stoi(line);break;
+                case 3: cropX1 = std::stoi(line);break;
+                case 4: cropY1 = std::stoi(line);break;
+            }
+            if( rowNum >= 0 ) rowNum++;
+        }
+        myfile.close();
+    }
+    else std::cout << "Unable to open file";
+}

+ 38 - 0
PageNormalizer/pagenormalizer.h

@@ -0,0 +1,38 @@
+#ifndef PAGENORMALIZER_H
+#define PAGENORMALIZER_H
+
+#define cimg_use_jpeg
+#include "CImg.h"
+using namespace cimg_library;
+
+
+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);
+
+
+
+
+private:
+    void ReadSettings(const std::string inFile);
+    void NormalizeImage();
+
+    int GetWhiteness(unsigned int x,unsigned int y);
+    int GetBlackness(unsigned int x,unsigned int y);
+    
+    CImg<unsigned char> inImage;
+    CImg<unsigned char> refImages[2];    // One for white(0) and one for black(1)
+    CImg<unsigned char> outImage;
+
+    int width;
+    int height;
+
+    int cropX0;
+    int cropY0;
+    int cropX1;
+    int cropY1;
+};
+
+#endif

File diff suppressed because it is too large
+ 61029 - 0
RefImageCreator/CImg.h


+ 21 - 0
RefImageCreator/RefImageCreator.pro

@@ -0,0 +1,21 @@
+TEMPLATE = app
+CONFIG += console c++11
+CONFIG -= app_bundle
+CONFIG -= qt
+
+QMAKE_CXXFLAGS_RELEASE -= -O
+QMAKE_CXXFLAGS_RELEASE -= -O1
+QMAKE_CXXFLAGS_RELEASE -= -O2
+QMAKE_CXXFLAGS_RELEASE -= -O3
+QMAKE_CXXFLAGS_RELEASE += -O2
+
+QMAKE_LIBDIR += /usr/X11R6/lib
+
+SOURCES += main.cpp \
+    imgmanipbs.cpp
+
+HEADERS += \
+    CImg.h \
+    imgmanipbs.h
+
+LIBS += -lm -lpthread -lX11

+ 4 - 0
RefImageCreator/build-Debug/.gitignore

@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore

+ 34 - 0
RefImageCreator/build-Mac/makefile

@@ -0,0 +1,34 @@
+# Folders
+SRCDIR := ..
+BUILDDIR := .
+TARGETDIR := .
+
+CC := clang++ -arch x86_64
+
+# Targets
+EXECUTABLE := RefImageCreator
+TARGET := ./$(EXECUTABLE)
+
+# Code Lists
+SRCEXT := cpp
+SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
+OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
+
+# Shared Compiler Flags
+CFLAGS := -c -Dcimg_display=0
+INC := -I..
+LIB := -lm -ljpeg
+
+INCDIRS := 
+INCLIST := 
+BUILDLIST := 
+
+CFLAGS += -std=c++11 -stdlib=libc++ -O2
+
+$(TARGET): $(OBJECTS)
+	@echo "Linking..."
+	@echo "  Linking $(TARGET)"; $(CC) $^ -o $(TARGET) $(LIB)
+
+$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
+	@echo "Compiling $<..."; $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+

+ 4 - 0
RefImageCreator/build-Release/.gitignore

@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore

BIN
RefImageCreator/example_imgs/ref_A.JPG


BIN
RefImageCreator/example_imgs/ref_B.JPG


BIN
RefImageCreator/example_imgs/ref_OLD.jpg


+ 167 - 0
RefImageCreator/imgmanipbs.cpp

@@ -0,0 +1,167 @@
+#include "imgmanipbs.h"
+#include <list>
+
+ImgManipBS::ImgManipBS() {
+    width = height = 0;
+}
+
+ImgManipBS::ImgManipBS(const char* fileName)
+{
+
+    mainImage = CImg<>(fileName);
+
+    width = mainImage.width();
+    height = mainImage.height();
+
+    noOfWSquares = width / squareSize;
+    noOfHSquares = height / squareSize;
+
+    whiteMeanValues = new int*[noOfWSquares];
+    blackMeanValues = new int*[noOfWSquares];
+
+    for (int i = 0; i < noOfWSquares; ++i) {
+      whiteMeanValues[i] = new int[noOfHSquares];
+      blackMeanValues[i] = new int[noOfHSquares];
+    }
+
+    // Output image
+    outImages[0] = CImg<>(width,height); // B&W Image with same size: White
+    outImages[1] = CImg<>(width,height); // B&W Image with same size: Black
+
+    printf("Original image size %dx%d\n",width,height);
+
+    EvaluateImage();
+
+    CreateGradiantImage();
+
+    outImages[0].save_jpeg("ref_white.jpg");
+    outImages[1].save_jpeg("ref_black.jpg");
+
+    printf("Done.\n");
+}
+
+void ImgManipBS::CreateGradiantImage() {
+
+    for(auto yy = 0; yy < noOfHSquares-1; yy++)
+    {
+        for (auto xx = 0; xx < noOfWSquares-1; xx++ )
+        {
+            int x = (xx*squareSize) + squareMargin;
+            int y = (yy*squareSize) + squareMargin;
+
+            DrawGradRect(0, x,y,whiteMeanValues[xx][yy],whiteMeanValues[xx+1][yy],whiteMeanValues[xx][yy+1],whiteMeanValues[xx+1][yy+1]);
+            DrawGradRect(1, x,y,blackMeanValues[xx][yy],blackMeanValues[xx+1][yy],blackMeanValues[xx][yy+1],blackMeanValues[xx+1][yy+1]);
+
+            if( yy == 0 )   // Only top row
+            {
+                DrawTopGradRect(0,x,0,whiteMeanValues[xx][yy],whiteMeanValues[xx+1][yy]);
+                DrawTopGradRect(1,x,0,blackMeanValues[xx][yy],blackMeanValues[xx+1][yy]);
+            }
+        }
+    }
+}
+
+void ImgManipBS::DrawGradRect(int imageNo,int x,int y, unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
+{
+    CImg<unsigned char> tempImage = CImg<>(squareSize,squareSize);  // Black and white image
+    const unsigned char white[] = { 255,255,255 };
+    const float opacity=1.0;
+
+    float fp1 = (1.0 / 255) * p1;
+    float fp2 = (1.0 / 255) * p2;
+    float fp3 = (1.0 / 255) * p3;
+    float fp4 = (1.0 / 255) * p4;
+
+    tempImage.draw_triangle(0,0,0,squareSize,squareSize,squareSize,white,fp1,fp3,fp4,opacity);
+    tempImage.draw_triangle(0,0,squareSize,0,squareSize,squareSize,white,fp1,fp2,fp4,opacity);
+
+    outImages[imageNo].draw_image(x,y,tempImage);
+}
+
+void ImgManipBS::DrawTopGradRect(int imageNo,int x,int y, unsigned char p1, unsigned char p2)
+{
+    CImg<unsigned char> tempImage = CImg<>(squareSize,squareMargin);  // Black and white image
+    const unsigned char white[] = { 255,255,255 };
+    const float opacity=1.0;
+
+    float fp1 = (1.0 / 255) * p1;
+    float fp2 = (1.0 / 255) * p2;
+
+    tempImage.draw_triangle(0,0,0,squareSize,squareSize,squareSize,white,fp1,fp1,fp2,opacity);
+    tempImage.draw_triangle(0,0,squareSize,0,squareSize,squareSize,white,fp1,fp2,fp2,opacity);
+
+    outImages[imageNo].draw_image(x,y,tempImage);
+}
+
+void ImgManipBS::EvaluateImage() {
+
+
+
+    printf("Starting to evaluate image....\n\n");
+
+    for(auto yy = 0; yy < noOfHSquares; yy++)
+    {
+        for (auto xx = 0; xx < noOfWSquares; xx++ )
+        {
+            int x = (xx*squareSize) + squareMargin;
+            int y = (yy*squareSize) + squareMargin;
+
+            double mW,mB;
+            findNearValues(x,y,mW,mB);
+
+            whiteMeanValues[xx][yy] = mW;
+            blackMeanValues[xx][yy] = mB;
+
+            //printf("%d,%d #%x%x%x ",x,y,(int)mW,(int)mW,(int)mW);*/
+
+            //MarkPixelInOutImage(0, x,y);
+        }
+    }
+    printf("\n");
+}
+
+void ImgManipBS::findNearValues(int x,int y, double &meanWhite, double &meanBlack) {
+
+    std::list <double> brList;
+
+    for(auto yy=y-(measureSquareSize/2);yy<=y+(measureSquareSize/2);yy++) {
+        for(auto xx=x-(measureSquareSize/2);xx<=x+(measureSquareSize/2);xx++) {
+            double br = GetBrightness(xx,yy);
+            brList.push_back( br );
+            //MarkPixelInOutImage(0, xx,yy);
+        }
+    }
+    brList.sort();
+
+    // Iterate through the list from the front (most black)
+    std::list<double>::iterator it=brList.begin();
+    double brSum=0;
+    int cnt=0;
+    for( auto i=0; i < noOfMeanValues && it != brList.end(); i++ ) {
+        brSum += *it;
+        cnt++;
+        it++;
+    }
+    meanBlack = brSum / cnt;
+
+    // Iterate through the list from the end (most white)
+    it=brList.end();
+    brSum=0;
+    cnt=0;
+    for( auto i=0; i < noOfMeanValues && it != brList.begin(); i++ ) {
+        brSum += *it;
+        cnt++;
+        it--;
+    }
+    meanWhite = brSum / cnt;
+}
+
+void ImgManipBS::MarkPixelInOutImage(int imageNo,int x,int y) {
+    outImages[imageNo](x,y,0,0) = 255;
+    outImages[imageNo](x,y,0,1) = 255;
+    //outImages[imageNo](x,y,0,2) = 255;
+}
+
+double ImgManipBS::GetBrightness(unsigned int x,unsigned int y) {
+    return( (mainImage(x,y,0,0)+mainImage(x,y,0,1)+mainImage(x,y,0,2))/3 );
+}

+ 43 - 0
RefImageCreator/imgmanipbs.h

@@ -0,0 +1,43 @@
+#ifndef IMGMANIPBS_H
+#define IMGMANIPBS_H
+
+#define cimg_use_jpeg
+#include "CImg.h"
+using namespace cimg_library;
+
+class ImgManipBS
+{
+public:
+    ImgManipBS();
+    ImgManipBS(const char* fileName);
+
+    CImg<unsigned char> mainImage;
+    CImg<unsigned char> outImages[2];    // One for white(0) and one for black(1)
+
+    int width;
+    int height;
+
+    int **whiteMeanValues;
+    int **blackMeanValues;
+
+
+    void EvaluateImage();
+    double GetBrightness(unsigned int x,unsigned int y);
+    void findNearValues(int x,int y, double &meanWhite, double &meanBlack);
+    void MarkPixelInOutImage(int imageNo,int x,int y);
+    void DrawGradRect(int imageNo,int x,int y, unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4);
+    void DrawTopGradRect(int imageNo,int x,int y, unsigned char p1, unsigned char p2);
+    void CreateGradiantImage();
+
+    int noOfWSquares;
+    int noOfHSquares;
+
+    // Constants that affect the sizes of differents measurements in the image process
+    const int squareSize = 140;
+    const int measureSquareSize = 60;
+    const int squareMargin = measureSquareSize / 2;
+
+    const int noOfMeanValues = 10;
+};
+
+#endif // IMGMANIPBS_H

+ 30 - 0
RefImageCreator/main.cpp

@@ -0,0 +1,30 @@
+#include <iostream>
+
+#include "CImg.h"
+using namespace cimg_library;
+
+#include "imgmanipbs.h"
+
+using namespace std;
+
+#ifndef cimg_imagepath
+#define cimg_imagepath "img/"
+#endif
+
+
+int main(int argc, char *argv[])
+{
+    // Read input image filename from the command line (or set it to "./ref.jpg" if option '-i' is not provided).
+    std::string file_i( cimg_option("-i","./ref.jpg","Input image") );
+    
+    // Read output foldername from the command line (or set it to "./" if option '-o' is not provided).
+    std::string out_folder( cimg_option("-o","./","Output folder") );
+    
+    ImgManipBS im = ImgManipBS(file_i.c_str());
+
+
+    return 0;
+}
+
+
+

+ 4 - 0
RefImageCreator/output/.gitignore

@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore

+ 149 - 0
TestProject/build.py

@@ -0,0 +1,149 @@
+#!/usr/bin/python
+
+#--------------------------------------------------------------------------------------------------------
+# This python file is processing a complete book
+# It assumes that there is a folder named 'input' that contains all input data
+# The output is generated into a folder called 'output'
+# A folder called 'temp' is also used for intermediate files (that can be cleaned later)
+# In the 'input'-folder, there should only be image-files ending with a jpg-file-extension
+#
+# To scan a book, start with all the right hand pages from the start to the end, then take all left hand pages
+# The number of right/left-pages must be equal
+#
+# The reference image and cover image can be taken in any order, but must be pointed out below in the 'arguments'
+#
+# Static Input arguments. 
+ipath = './input'
+opath = './output'
+tpath = './temp'
+settingsFile = tpath + '/' + 'settings.txt'
+image_pre_name = 'img_'         # The part of the filename before the numerical part
+image_post_name = '.jpg'        # Part of the filename after the numerical part
+numberNumericalsInName=4        # Number of numericals in file name-index. For example 4 in 0304 and 1234
+
+# Dynamic Input arguments. These very for every book that is scanned and for every scan
+refImg = 100                    # The image used as reference image
+coverImage = 238.               # Image used for cover (Will be placed 1st)
+firstLeftHandPage = 237         # The first image covering the left pages
+cropX1 = 250                    # Crop coordinates of all the images
+cropY1 = 38
+cropX2 = 3673
+cropY2 = 2685
+
+#--------------------------------------------------------------------------------------------------------
+import os
+import subprocess
+import shutil
+
+# Do some clean-up first of folders
+shutil.rmtree(tpath, ignore_errors=True)
+shutil.rmtree(opath, ignore_errors=True)
+#subprocess.call('rm -rf ' + tpath)
+#subprocess.call('rm -rf ' + opath)
+
+# Create output folders, if they don't exist
+if not os.path.exists(opath): os.makedirs(opath)
+if not os.path.exists(tpath): os.makedirs(tpath)
+
+if not os.path.exists(ipath):
+    print('ERROR: No input folder !')
+    exit()
+    
+# 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
+pics = sorted([os.path.join(ipath, pic)
+               for pic in os.listdir(ipath)
+               if pic.endswith('.jpg') and os.path.isfile(os.path.join(ipath, pic))])
+
+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 = ''
+
+for ifile in pics:
+    imgNoStartPos = ifile.find(image_pre_name)+len(image_pre_name)
+    imgNumber = int( ifile[imgNoStartPos:imgNoStartPos+numberNumericalsInName] )
+    # Check if it's the cover image or reference image
+    if ifile.find( covFile ) > 0:
+        covFilePath = ifile
+        print( 'Cover file:' + ifile )
+    elif ifile.find( refFile ) > 0:
+        refFilePath = ifile
+        print( 'Reference file:' + ifile )
+    elif imgNumber < firstLeftHandPage:
+        rHandSides.append( imgNumber )
+    elif imgNumber >= firstLeftHandPage:
+        lHandSides.append( imgNumber )
+        
+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()
+    
+if refFilePath == '' or covFilePath == '':
+    print('ERROR: Missing cover or reference image')
+    exit()
+
+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
+
+print( '---------- Processing cover page  ----------' )
+
+# First convert the cover-page as it should be number one
+subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
+                ' -i ' + covFilePath + \
+                ' -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)
+
+# Increase the page conter
+pageNo = pageNo + 1
+
+# Start doing the rest of the pages. Left pages first and then the right page
+for i in range( len(lHandSides) ):    
+    
+    # Left page first. Process it
+    print( '---------- Processing left-hand page: ' + str(pageNo) + ' ----------' )
+    subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
+                    ' -i ' + ipath + '/' + image_pre_name + str(lHandSides[i]).zfill(numberNumericalsInName) + image_post_name  + \
+                    ' -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)
+
+
+    pageNo = pageNo + 1 # Increase the page conter
+    
+    # Right page after. Process it
+    print( '---------- Processing right-hand page: ' + str(pageNo) + ' ----------' )
+    subprocess.call('../PageNormalizer/build-Mac/PageNormalizer ' + \
+                    ' -i ' + ipath + '/' + image_pre_name + str(rHandSides[i]).zfill(numberNumericalsInName) + image_post_name  + \
+                    ' -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
+    
+    

BIN
TestProject/input/img_0100.jpg


BIN
TestProject/input/img_0234.jpg


BIN
TestProject/input/img_0235.jpg


BIN
TestProject/input/img_0236.jpg


BIN
TestProject/input/img_0237.jpg


BIN
TestProject/input/img_0238.jpg


BIN
TestProject/input/img_0239.jpg


BIN
TestProject/input/img_0240.jpg