Skip to content

Instantly share code, notes, and snippets.

@korc
Created August 16, 2018 05:00
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 korc/40fe49a7a9e8e28dcabcaed778f051a3 to your computer and use it in GitHub Desktop.
Save korc/40fe49a7a9e8e28dcabcaed778f051a3 to your computer and use it in GitHub Desktop.
#!/bin/sh
find_scanner() {
echo -n "Scanning for scanners.." >&2
scanimage -f %d
echo "OK." >&2
}
test ! "x$1" = "x-h" || {
echo "Usage: ${0##*/} [<scanner>]" >&2
exit 1
}
: "${scanner:=$1}"
: "${scanner:=$(find_scanner)}"
: "${dpi:=300}"
: "${mode:=Color}"
: "${preview:=no}"
: "${clip_w:=90}"
: "${clip_h:=54}"
if test -n "$scanner";then
echo "Using scanner: '$scanner'"
else
echo "Error: no scanner device found. Try 'scanimage -L' or something." >&2
exit 1
fi
test -n "$VIEWER" || {
if test -n "$DISPLAY";then
VIEWER="xdg-open"
else
VIEWER="ls -l"
fi
}
set -e
while true; do
echo "Clip size: ${clip_w:-None}${clip_w:+ x ${clip_h}}"
echo " q: quit"
echo " z: swap clip width / height (switch portrait/landscape)"
echo " <width>x<height>: set new width and height"
echo -n "Press Enter to scan or a command from above.. "
read enter
case "$enter" in
*[Xx]*)
clip_w="${enter%x*}"
clip_h="${enter#*x}"
continue
;;
[Zz])
tmp="$clip_w";clip_w="$clip_h";clip_h="$tmp"
continue
;;
[qQ]) exit;;
esac
outfile="$(date "+%y%m%d %H%M%S") ${clip_w}x${clip_h}.jpg"
echo "Output: $outfile"
if (set -x;
scanimage -d "$scanner" -p ${preview:+--preview=no} ${mode:+--mode "$mode"} ${dpi:+--resolution "$dpi"} --format jpeg ${clip_w:+-x "$clip_w"} ${clip_h:+-y "$clip_h"} >"$outfile"
) && test -s "$outfile";then
$VIEWER "$outfile"
else
echo "Failed?"
rm -f "$outfile"
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment