Skip to content

Instantly share code, notes, and snippets.

@turesheim
Last active December 27, 2015 08:09
Show Gist options
  • Save turesheim/7294079 to your computer and use it in GitHub Desktop.
Save turesheim/7294079 to your computer and use it in GitHub Desktop.
Bash script to scan documents to PDF.
#!/bin/bash
#-----------------------------------------------------------------------------
# pixma2pdf.sh version 3.0
#
# Copyright (c) 2009, 2013 Torkild U. Resheim - [email protected]
# Released under the Eclipse Public License version 1.0
#-----------------------------------------------------------------------------
t=$(tempfile) || exit
trap "rm -f -- '$t'" EXIT
mkdir documents
title="Canon Pixma - Scan to PDF"
dialog --title "$title" --checklist "Scan options" 10 50 10 \
multi "Scan multiple documents" on \
mail "Send document in e-mail" off \
pause "Confirm between pages" off 2>$t
# Check for cancel
if [ $? == 1 ] ; then
exit 0
fi
options=$(cat $t)
if [[ "$options" =~ "mail" ]] ; then
dialog --title "$title" --inputbox "The address to mail to" 8 40 $address 2>$t
address=$(cat $t)
fi
dialog --title "$title" --radiolist "Scanner resolution" 11 40 10 \
75 "75dpi Screen" off \
150 "150dpi" off \
300 "300dpi Print" on \
600 "600dpi" off 2>$t
# Check for cancel
if [ $? == 1 ] ; then
exit 0
fi
resolution=$(cat $t)
dialog --title "$title" --radiolist "Page size" 11 40 10 \
a4 "A4" on \
a5 "A5" off \
b5 "B5" off \
letter "Letter" off 2>$t
# Check for cancel
if [ $? == 1 ] ; then
exit 0
fi
pages=1
paperSize=$(cat $t)
if [[ "$options" = "multi" ]] ; then
dialog --title "$title" --inputbox "The number of pages in document" 8 40 1 2>$t
pages=$(cat $t)
fi
# Check for cancel
if [ $? == 1 ] ; then
exit 0
fi
# Assume the scanner contains at least one sheet
sheets=1
while [ $sheets -eq 1 ] ; do
i=1
while [ $i -le $pages ] ; do
if [[ "$options" =~ "pause" ]] ; then
dialog --title "$title" --msgbox "Prepare to scan page $i" 8 40
fi
echo 10 | dialog --title "$title" --gauge "Scanning page $i of $pages" 8 40 0
# Perform the scan
sudo pixma_scan -a -r $resolution -v 1 -o scan_$i.pnm
# Exit if there was an error
if [ $? -ne 0 ] ; then
exit $?
fi
# Attempt to crop the image
pnmcrop -left -right -top -bottom -sides scan_$i.pnm > cropped_$i.pnm
# Remove the original image
sudo rm scan_$i.pnm
# Convert to PostScript
pnmtops -equalpixels -dpi=$resolution cropped_$i.pnm > scan_$i.ps
rm cropped_$i.pnm
# Test for more pages in the document
raspistill -roi 0.08,0.65,0.10,0.07 -w 256 -h 256 -br 90 -co 100 -mm average -ISO 100 -o camera.jpg
values=$(convert camera.jpg -threshold 50% -format %c histogram:info:- | tr -s ':' | cut -d ':' -f 1)
black=$(echo $values | cut -d ' ' -f 1)
white=$(echo $values | cut -d ' ' -f 2)
if [ $black -gt $white ] ; then
let pages++
fi
let i++
done
if [ $pages -gt 1 ] ; then
# Merge the PostScript files to one
gs -q -sPAPERSIZE=$paperSize -dNOPAUSE -sDEVICE=pswrite -dBATCH -sOutputFile=scan.ps *.ps
# Remove the temporary postscript files
for i in `seq $pages` ; do
rm scan_$i.ps
done
else
mv scan_1.ps scan.ps
fi
# Create the PDF file
filename="raspiscan_"$(date +"%Y-%m-%d-%T").pdf
ps2pdf -sPAPERSIZE=$paperSize scan.ps documents/$filename
# Clean up
rm scan.ps
# Mail the PDF file
if [[ "$options" =~ "mail" ]] ; then
echo "Your scanned document is attached" | mutt -s "Scanned document" -a documents/$filename -- $address
fi
# Test if there are more sheets
sudo pixma_scan -v 0 -t
if [ $? == 1 ] ; then
let sheets=0
fi
done
# Clean up the temporary dialog files
rm -f -- "$t"
trap - EXIT
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment