Introduce -s option + fix bug when -C no set
- Introduce -s option to no ocr pages containing fonts - Solve issue with -f and -s if -C is not set
This commit is contained in:
Executable → Regular
+18
-4
@@ -42,8 +42,10 @@ Usage: OCRmyPDF.sh [-h] [-v] [-g] [-k] [-d] [-c] [-i] [-o dpi] [-f] [-l languag
|
||||
-o : If the resolution of an image is lower than dpi value provided as argument, provide the OCR engine with
|
||||
an oversampled image having the latter dpi value. This can improve the OCR results but can lead to a larger output PDF file.
|
||||
(default: no oversampling performed)
|
||||
-f : Force to OCR the whole document, even if some page already contain font data
|
||||
(which should not be the case for PDF files built from scanned images)
|
||||
-f : Force to OCR the whole document, even if some page already contain font data.
|
||||
(which should not be the case for PDF files built from scnanned images)
|
||||
Any text data will be rendered to raster format and then fed through OCR.
|
||||
-s : If pages contain font data, do not OCR that page, but include the page (as is) in the final output.
|
||||
-l : Set the language of the PDF file in order to improve OCR results (default "eng")
|
||||
Any language supported by tesseract is supported (Tesseract uses 3-character ISO 639-2 language codes)
|
||||
Multiple languages may be specified, separated by '+' characters.
|
||||
@@ -85,10 +87,11 @@ PREPROCESS_CLEANTOPDF="0" # 0=no, 1=yes (put cleaned image in final PDF)
|
||||
OVERSAMPLING_DPI="0" # 0=do not perform oversampling (dpi value under which oversampling should be performed)
|
||||
PDF_NOIMG="0" # 0=no, 1=yes (generates each PDF page twice, with and without image)
|
||||
FORCE_OCR="0" # 0=do not force, 1=force (force to OCR the whole document, even if some page already contain font data)
|
||||
SKIP_TEXT="0" # 0=do not skip text pages, 1=skip text pages
|
||||
TESS_CFG_FILES="" # list of additional configuration files to be used by tesseract
|
||||
|
||||
# Parse optional command line arguments
|
||||
while getopts ":hvgkdcio:fl:C:" opt; do
|
||||
while getopts ":hvgkdcio:fsl:C:" opt; do
|
||||
case $opt in
|
||||
h) usage ; exit 0 ;;
|
||||
v) VERBOSITY=$(($VERBOSITY+1)) ;;
|
||||
@@ -99,6 +102,7 @@ while getopts ":hvgkdcio:fl:C:" opt; do
|
||||
i) PREPROCESS_CLEANTOPDF="1" ;;
|
||||
o) OVERSAMPLING_DPI="$OPTARG" ;;
|
||||
f) FORCE_OCR="1" ;;
|
||||
s) SKIP_TEXT="1" ;;
|
||||
l) LAN="$OPTARG" ;;
|
||||
C) TESS_CFG_FILES="$OPTARG $TESS_CFG_FILES" ;;
|
||||
\?)
|
||||
@@ -122,6 +126,14 @@ if [ "$#" -ne "2" ]; then
|
||||
exit $EXIT_BAD_ARGS
|
||||
fi
|
||||
|
||||
# Ensure that -f and -s are not both set
|
||||
if [ "$SKIP_TEXT" -eq "1" -a "$FORCE_OCR" -eq "1" ]; then
|
||||
echo "Options -f and -s are mutually exclusive; choose one or the other"
|
||||
usage
|
||||
exit $EXIT_BAD_ARGS
|
||||
fi
|
||||
|
||||
|
||||
[ ! -f "$1" ] \
|
||||
&& echo "The input file does not exist. Exiting..." && exit $EXIT_BAD_ARGS
|
||||
FILE_INPUT_PDF="`absolutePath "$1"`"
|
||||
@@ -145,6 +157,7 @@ cd "$BASEPATH"
|
||||
! command -v pdfimages > /dev/null && echo "Please install poppler-utils. Exiting..." && exit $EXIT_MISSING_DEPENDENCY
|
||||
! command -v pdftoppm > /dev/null && echo "Please install poppler-utils. Exiting..." && exit $EXIT_MISSING_DEPENDENCY
|
||||
! command -v pdffonts > /dev/null && echo "Please install poppler-utils. Exiting..." && exit $EXIT_MISSING_DEPENDENCY
|
||||
! command -v pdfseparate > /dev/null && echo "Please install or update poppler-utils to at least 0.24.5. Exiting..." && exit $EXIT_MISSING_DEPENDENCY
|
||||
[ $PREPROCESS_CLEAN -eq 1 ] && ! command -v unpaper > /dev/null && echo "Please install unpaper. Exiting..." && exit $EXIT_MISSING_DEPENDENCY
|
||||
! command -v tesseract > /dev/null && echo "Please install tesseract and tesseract-data. Exiting..." && exit $EXIT_MISSING_DEPENDENCY
|
||||
! python2 -c 'import lxml' 2>/dev/null && echo "Please install the python library lxml. Exiting..." && exit $EXIT_MISSING_DEPENDENCY
|
||||
@@ -190,6 +203,7 @@ if [ $VERBOSITY -ge $LOG_DEBUG ]; then
|
||||
pdfimages -v
|
||||
pdftoppm -v
|
||||
pdffonts -v
|
||||
pdfseparate -v
|
||||
echo "--------------------------------"
|
||||
echo "unpaper version:"
|
||||
unpaper --version
|
||||
@@ -258,7 +272,7 @@ numpages=`tail -n 1 "$FILE_PAGES_INFO" | cut -f1 -d" "`
|
||||
# process each page of the input pdf file
|
||||
parallel --gnu -q -k --halt-on-error 1 "$OCR_PAGE" "$FILE_INPUT_PDF" "{}" "$numpages" "$TMP_FLD" \
|
||||
"$VERBOSITY" "$LAN" "$KEEP_TMP" "$PREPROCESS_DESKEW" "$PREPROCESS_CLEAN" "$PREPROCESS_CLEANTOPDF" "$OVERSAMPLING_DPI" \
|
||||
"$PDF_NOIMG" "$TESS_CFG_FILES" "$FORCE_OCR" < "$FILE_PAGES_INFO"
|
||||
"$PDF_NOIMG" "$FORCE_OCR" "$SKIP_TEXT" "$TESS_CFG_FILES" < "$FILE_PAGES_INFO"
|
||||
ret_code="$?"
|
||||
[ $ret_code -ne 0 ] && exit $ret_code
|
||||
|
||||
|
||||
+12
-5
@@ -21,8 +21,10 @@ PREPROCESS_CLEAN="$9" # Clean the page to be OCRed
|
||||
PREPROCESS_CLEANTOPDF="${10}" # Put the cleaned paged in the OCRed PDF
|
||||
OVERSAMPLING_DPI="${11}" # Oversampling resolution in dpi
|
||||
PDF_NOIMG="${12}" # Request to generate also a PDF page containing only the OCRed text but no image (helpful for debugging)
|
||||
TESS_CFG_FILES="${13}" # Specific configuration files to be used by Tesseract during OCRing
|
||||
FORCE_OCR="${14}" # Force to OCR, even if the page already contains fonts
|
||||
FORCE_OCR="${13}" # Force to OCR, even if the page already contains fonts
|
||||
SKIP_TEXT="${14}" # Skip OCR on pages that contain fonts and include the page anyway
|
||||
TESS_CFG_FILES="${15}" # Specific configuration files to be used by Tesseract during OCRing
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -119,9 +121,14 @@ dpi=$DEFAULT_DPI # default resolution
|
||||
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" ] && [ "$FORCE_OCR" -eq "0" ]); then
|
||||
echo "Page $page: Exiting... (Use the -f option to force OCRing, even though fonts are available in the input file)" && exit $EXIT_BAD_INPUT_FILE
|
||||
|
||||
# Handle pages that already contain a text layer
|
||||
if ([ "$ret_code" -eq "1" ] && [ "$FORCE_OCR" -eq "0" ] && [ "$SKIP_TEXT" -eq "0" ]); then
|
||||
echo "Page $page: Exiting... (Use the -f option to force OCR, even though fonts are available in the input file. Or use -s to include the page as is (no OCR) in the output file)" && exit $EXIT_BAD_INPUT_FILE
|
||||
elif ([ "$ret_code" -eq "1" ] && [ "$SKIP_TEXT" -eq "1" ]); then
|
||||
[ $VERBOSITY -ge $LOG_INFO ] && echo "Page $page: Skipping processing because page contains text..."
|
||||
pdfseparate -f $page -l $page "${FILE_INPUT_PDF}" "$curOCRedPDF"
|
||||
exit 0
|
||||
elif ([ "$ret_code" -eq "1" ] && [ "$FORCE_OCR" -eq "1" ]); then
|
||||
[ $VERBOSITY -ge $LOG_WARN ] && echo "Page $page: OCRing anyway, assuming a default resolution of $dpi dpi"
|
||||
# in case the page contains more than one image, warn the user but go on with default parameters
|
||||
|
||||
Reference in New Issue
Block a user