Instantly share code, notes, and snippets.

Embed
What would you like to do?
scan some paper, put it through ocr and convert everything to a searchable pdf
#!/usr/bin/fish
# change this to your device id, see scanimage -L for a list of your devices
set -x device 'epjitsu:libusb:001:056'
# exit if no title is provided
if not set -q argv[1]:
echo -e "Please enter at least a title!\n\nArguments:\n ./scan.fish \"title\" source resolution mode destinationdir"
exit 1
end
# simply convert arguments to variables
if set -q argv[1]
set -x title (echo $argv[1])
end
if set -q argv[2]
set -x source (echo $argv[2])
else
set -x source "ADF Front"
end
if set -q argv[3]
set -x resolution (echo $argv[3])
else
set -x resolution 300
end
if set -q argv[4]
set -x mode $argv[4]
else
set -x mode Gray
end
if set -q argv[5]
set -x destinationdir $argv[5]
else
set -x destinationdir '/home/rullmann/Downloads'
end
# create temporary dir with variable name
set -x tempdir /tmp/scan_(tr -dc 'a-z0-9' < /dev/urandom | head -c 32)
mkdir $tempdir
# create output filename for final pdf by converting the title and adding the date
set -x outputfile $destinationdir/(date +%F)_(echo $title | sed -e 's/\(.*\)/\L\1/' -e 's/\ /_/g').pdf
# actually scan and process the input
scanadf -d $device --resolution $resolution --mode $mode --source "$source" -o $tempdir/%d ;and for file in (ls $tempdir/) ; pnmtops $tempdir/$file ; end | ps2pdf - | ocrmypdf -l deu+eng --rotate-pages --deskew - $outputfile --title "$title"
# remove temp dir
rm -r $tempdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment