Skip to content

Instantly share code, notes, and snippets.

@marcosrogers
Forked from jacksenechal/ocr-script.sh
Last active December 27, 2025 12:16
Show Gist options
  • Select an option

  • Save marcosrogers/fc0250a52490e92ab8293bd781231a7e to your computer and use it in GitHub Desktop.

Select an option

Save marcosrogers/fc0250a52490e92ab8293bd781231a7e to your computer and use it in GitHub Desktop.
Gnome simple-scan post processing script to run OCR with ocrmypdf
#!/bin/bash
# ocr-script.sh
#
# Usage:
# Set this file as the post-processing script in the simple-scan preferences. No extra arguments needed.
# Any postprocessing script arguments entered in the preferences will be passed along to ocrmypdf, for
# example, add -l=eng+spa to recognize English and Spanish text.
#
# Requirements:
# - simple-scan
# - ocrmypdf
#
# For reference, at the time of writing the arguments from simple-scan are:
# $1 - the mime type, eg application/pdf
# $2 - whether or not to keep a copy of the original file
# $3 - the filename
# $4..N - postprocessing script arguments entered in preferences
filename=$3
keep_original=$2
if [[ "$keep_original" == "true" ]]; then
ocr_filename="${filename%\.*}.ocr.${filename##*\.}"
extra_msg_details="Saved as ${ocr_filename##*\/}.\nOriginal saved as ${filename##*\/}."
else
extra_msg_details="Saved as ${filename##*\/}."
fi
/usr/bin/ocrmypdf --deskew --clean --force-ocr ${@:4} "$filename" "${ocr_filename-$filename}" &> /tmp/ocr.log
if [ $? -ne 0 ]; then
notify-send -i scanner "OCR Failed" "See /tmp/ocr.log"
exit 1
fi
notify-send -i scanner "OCR Complete" "$extra_msg_details"
@sparr
Copy link

sparr commented Feb 23, 2025

Your line 28 is an error if no additional command line parameters are provided. It results in an empty-string parameter which ocrmypdf interprets as the input file name.

@marcosrogers
Copy link
Author

Thanks, @sparr I've updated the gist to deal with that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment