fixes #40 and code cleanup
This commit is contained in:
+14
-14
@@ -34,16 +34,16 @@ FORCE_OCR="${14}" # Force to OCR, even if the page already contains fonts
|
||||
# Param 2: PDF page width in pt
|
||||
# Param 3: PDF page height in pt
|
||||
# Param 4: temporary file path (Path of the file in which the output should be written)
|
||||
# Output: A file (<pagenum>-img-characteristics.txt) containing the characteristics of the embedded image
|
||||
# Structure of the file:
|
||||
# <dpi> <colorspace>
|
||||
# Output: A file containing the characteristics of the embedded image. File structure:
|
||||
# DPI=<dpi>
|
||||
# COLOR_SPACE=<colorspace>
|
||||
# Returns:
|
||||
# - 0: if no error occurs
|
||||
# - 1: in case the page already contains fonts (which should be the case for PDF generated from scanned pages)
|
||||
# - 2: in case the page contains more than one image
|
||||
##################################
|
||||
getImgInfo() {
|
||||
local page widthPDF heightPDF curImgCharacteristics nbImg curImg propCurImg widthCurImg heightCurImg colorspaceCurImg dpi
|
||||
local page widthPDF heightPDF curImgInfo nbImg curImg propCurImg widthCurImg heightCurImg colorspaceCurImg dpi
|
||||
|
||||
# page number
|
||||
page="$1"
|
||||
@@ -51,7 +51,7 @@ getImgInfo() {
|
||||
widthPDF="$2"
|
||||
heightPDF="$3"
|
||||
# path of the file in which the output should be written
|
||||
curImgCharacteristics="$4"
|
||||
curImgInfo="$4"
|
||||
|
||||
|
||||
[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Page $page: Size ${heightPDF}x${widthPDF} (h*w in pt)"
|
||||
@@ -85,7 +85,8 @@ getImgInfo() {
|
||||
dpi=`echo "scale=0;$dpi/1" | bc`
|
||||
|
||||
# save the image characteristics
|
||||
echo "$dpi $colorspaceCurImg" > $curImgCharacteristics
|
||||
echo "DPI=$dpi" > "$curImgInfo"
|
||||
echo "COLOR_SPACE=$colorspaceCurImg" >> "$curImgInfo"
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -104,11 +105,11 @@ curOrigImg="$TMP_FLD/${page}.orig-img" # original image available in the curr
|
||||
curHocr="$TMP_FLD/${page}.hocr" # hocr file to be generated by the OCR SW for the current page
|
||||
curOCRedPDF="$TMP_FLD/${page}.ocred.pdf" # PDF file containing the image + the OCRed text for the current page
|
||||
curOCRedPDFDebug="$TMP_FLD/${page}.ocred.todebug.pdf" # PDF file containing data required to find out if OCR worked correctly
|
||||
curImgCharacteristics="$TMP_FLD/${page}.orig-img-info.txt" # Detected characteristics of the embedded image
|
||||
curImgInfo="$TMP_FLD/${page}.orig-img-info.txt" # Detected characteristics of the embedded image
|
||||
|
||||
|
||||
# auto-detect the characteristics of the embedded image
|
||||
getImgInfo "$page" "$widthPDF" "$heightPDF" "$curImgCharacteristics"
|
||||
getImgInfo "$page" "$widthPDF" "$heightPDF" "$curImgInfo"
|
||||
ret_code="$?"
|
||||
# in case the page contains text do not OCR, unless the FORCE_OCR flag is set
|
||||
if [ "$ret_code" -eq "1" -a "$FORCE_OCR" -eq "0" ]; then
|
||||
@@ -124,12 +125,11 @@ elif [ "$ret_code" -eq "2" ]; then
|
||||
[ $VERBOSITY -ge $LOG_WARN ] && echo "Page $page: Continuing anyway, assuming a default resolution of $dpi dpi"
|
||||
else
|
||||
# read the image characteristics from the file
|
||||
dpi=`cat "$curImgCharacteristics" | cut -f1 -d" "`
|
||||
colorspaceCurImg=`cat "$curImgCharacteristics" | cut -f2 -d" "`
|
||||
dpi=`cat "$curImgInfo" | grep "^DPI=" | cut -f2 -d"="`
|
||||
colorspaceCurImg=`cat "$curImgInfo" | grep "^COLOR_SPACE=" | cut -f2 -d"="`
|
||||
fi
|
||||
|
||||
# perform oversampling if the resolution is not big enough
|
||||
# to get good OCR results
|
||||
# perform oversampling if the resolution is not sufficient to get good OCR results
|
||||
if [ "$dpi" -lt "$OVERSAMPLING_DPI" ]; then
|
||||
[ $VERBOSITY -ge $LOG_WARN ] && echo "Page $page: Low image resolution detected ($dpi dpi). Performing oversampling ($OVERSAMPLING_DPI dpi) to try to get better OCR results."
|
||||
dpi="$OVERSAMPLING_DPI"
|
||||
@@ -148,7 +148,7 @@ curImgPixmap="$TMP_FLD/$page.$ext"
|
||||
curImgPixmapDeskewed="$TMP_FLD/$page.deskewed.$ext"
|
||||
curImgPixmapClean="$TMP_FLD/$page.cleaned.$ext"
|
||||
|
||||
# extract current page as image with right orientation and resolution
|
||||
# extract current page as image with correct orientation and resolution
|
||||
[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Page $page: Extracting image as $ext file (${dpi} dpi)"
|
||||
! pdftoppm -f $page -l $page -r $dpi $opt "$FILE_INPUT_PDF" > "$curImgPixmap" \
|
||||
&& echo "Could not extract page $page as $ext from \"$FILE_INPUT_PDF\". Exiting..." && exit $EXIT_OTHER_ERROR
|
||||
@@ -206,7 +206,7 @@ if [ $KEEP_TMP -eq 0 ]; then
|
||||
rm -f "$curImgPixmap"
|
||||
rm -f "$curImgPixmapDeskewed"
|
||||
rm -f "$curImgPixmapClean"
|
||||
rm -f "$curImgCharacteristics"
|
||||
rm -f "$curImgInfo"
|
||||
fi
|
||||
|
||||
exit 0
|
||||
Reference in New Issue
Block a user