Skip to content

Instantly share code, notes, and snippets.

@grahamc

grahamc/scan.sh Secret

Last active November 11, 2019 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamc/7550de3b490598439558e6840fb59204 to your computer and use it in GitHub Desktop.
Save grahamc/7550de3b490598439558e6840fb59204 to your computer and use it in GitHub Desktop.
scan all the fronts, then the backs. stitch them together and ocrmypdf them.
#!/usr/bin/env nix-shell
#!nix-shell -I nixpkgs=/home/grahamc/projects/github.com/NixOS/nixpkgs
#!nix-shell -i bash -p sane-frontends netpbm imagemagick poppler_utils ocrmypdf
set -x
make_pdfs() (
scanadf \
-d pixma:04A91774_21C8AC \
--source "Automatic Document Feeder" \
--mode color \
--resolution 300 \
-l auto \
-t auto
rm ./*.ps ./*.png ./*.pdf
for f in $(find -type f -name 'image-*' | sort -n); do
if ! pnmtopng "$f" > "$f.png" ; then
rm "$f.png"
continue
fi
convert "$f.png" "$f.pdf"
rm "$f.png"
done
)
(
cd fronts
make_pdfs
)
(
cd backs
make_pdfs
)
(
mkdir unite
cd unite
i=0
while true; do
i=$(( i + 1 ))
front=$(find ../fronts -type f -name '*.pdf' | sort -n | head -n1)
if [ "x$front" != "x" ]; then
mv "$front" "./page-$i.pdf"
fi
i=$(( i + 1 ))
back=$(find ../backs -type f -name '*.pdf' | sort -nr | head -n1)
if [ "x$back" != "x" ]; then
mv "$back" "./page-$i.pdf"
else
break
fi
done
)
pdfunite $(find ./unite -type f -name '*.pdf' | sort -n) ./united.pdf
ocrmypdf \
--clean \
--rotate-pages \
--deskew \
--remove-background \
--sidecar ./scan.txt \
./united.pdf \
./output.pdf
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment