multiscan.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/sh
  2. die()
  3. {
  4. echo "$*"
  5. exit 1
  6. }
  7. usage()
  8. {
  9. echo "Usage: $0 [options] filename-base [scanimage options]"
  10. echo
  11. echo "Options:"
  12. echo " --startcount XXX Start filename count at XXX"
  13. echo " --resolution XXX Scan resolution in DPI"
  14. echo " --format XXX Set output format. Default jpg"
  15. echo " --quality XX Output quality. Default 90"
  16. exit 1
  17. }
  18. [ $# -ge 1 ] || usage
  19. count=0
  20. resolution=
  21. out_format="jpg"
  22. out_quality="90"
  23. while [ $# -ge 1 ]; do
  24. case "$1" in
  25. -h|--help)
  26. usage
  27. ;;
  28. --startcount)
  29. shift
  30. count="$1"
  31. expr "$count" + 1 >/dev/null 2>&1 ||\
  32. die "--startcount is not numeric"
  33. ;;
  34. --format)
  35. shift
  36. out_format="$1"
  37. ;;
  38. --resolution)
  39. shift
  40. resolution="$1"
  41. ;;
  42. --quality)
  43. shift
  44. out_quality="$1"
  45. ;;
  46. *)
  47. break
  48. ;;
  49. esac
  50. shift
  51. done
  52. filename_base="$1"
  53. shift
  54. [ -n "$filename_base" ] || die "No filename base"
  55. cont_prompt()
  56. {
  57. printf '\nGoing to scan page %03d\n' "$count"
  58. [ $first -eq 0 ] && {
  59. read -p "[Press enter to continue or ^C to abort]" RES
  60. }
  61. first=0
  62. }
  63. do_scanimage()
  64. {
  65. echo "Running: scanimage $* > $filename" >&2
  66. scanimage "$@" || die "scanimage failed"
  67. }
  68. find_device()
  69. {
  70. scanimage -L | while read line; do
  71. echo "$line" | grep -qEe '(/dev/video)|(Failed cupsGetDevices)' || {
  72. echo -n "$line" | cut -d'`' -f2 | cut -d\' -f1
  73. break
  74. }
  75. done
  76. }
  77. echo "Searching for scanner..."
  78. dev="$(find_device)"
  79. [ -n "$dev" ] || die "Did not find a scanner"
  80. echo "Using scanner '$dev'"
  81. first=1
  82. while cont_prompt; do
  83. filename="$(printf '%s-%03d.pnm' "$filename_base" "$count")"
  84. opts=
  85. [ -n "$resolution" ] && opts="$opts --resolution $resolution"
  86. do_scanimage -d "$dev" \
  87. --mode Color \
  88. -l 0 -t 0 -x 210 -y 297 \
  89. --format=pnm \
  90. $opts \
  91. "$@" > "$filename"
  92. [ "$out_format" = "jpg" ] && {
  93. jpg_filename="$(basename "$filename" .pnm).jpg"
  94. echo "Converting to '$jpg_filename'..."
  95. convert pnm:"$filename" \
  96. -quality "$out_quality" jpg:"$jpg_filename" ||\
  97. die "Failed to convert image to jpg"
  98. rm "$filename"
  99. }
  100. count="$(expr "$count" + 1)"
  101. done