scan-dialog 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/env bash
  2. export GDK_BACKEND=x11
  3. export SANE_DEFAULT_DEVICE='epsonds:libusb:001:006' # Specify device to avoid startup delays and scanning slowness.
  4. function check_filename {
  5. path=$1
  6. folder=$(dirname "$path")
  7. file=$(basename "$path")
  8. extension="${file##*.}"
  9. filename="${file%.*}"
  10. if [[ -e "$path" ]]; then
  11. i=2
  12. while [[ -e "$folder/$filename-$i.$extension" ]]; do
  13. let i++
  14. done
  15. filename=$filename-$i
  16. fi
  17. echo "$folder/$filename.$extension"
  18. }
  19. export -f check_filename # Make available to run_scan's command substitution
  20. # positional arguments: 1-folder 2-prefix 3-format 4-papersize 5-resolution 6-colormode
  21. function run_scan {
  22. # Paper size.
  23. case $4 in
  24. A4)
  25. x=210
  26. y=297
  27. ;;
  28. A5)
  29. x=148
  30. y=210
  31. ;;
  32. A5R)
  33. x=210 # currently removed A5R because sane epson backend throws an out of memory error.
  34. y=148
  35. ;;
  36. esac
  37. filename=$(check_filename $1/$2.$3)
  38. echo "$6 $5 $x $y $filename"
  39. tempname="./temp-$(date +%s).png"
  40. scanimage --mode=$6 --resolution=$5 -x $x -y $y --format=png --output-file=$tempname | \
  41. yad --progress --text="Scanning to PNG..." --width=300 --no-buttons --auto-close
  42. case $3 in
  43. png)
  44. mv $tempname $filename
  45. ;;
  46. jpg)
  47. convert -quality 85 $tempname $filename | \
  48. yad --progress --text="Converting to $3..." --width=300 --no-buttons --auto-close
  49. rm $tempname
  50. ;;
  51. pdf)
  52. if [[ $6 != "Lineart" ]] then
  53. tempname2="${tempname%.png}.jpg"
  54. convert -quality 85 $tempname $tempname2 | \
  55. yad --progress --text="Converting to jpg..." --width=300 --no-buttons --auto-close
  56. rm $tempname
  57. tempname=$tempname2
  58. fi
  59. convert $tempname $filename | \
  60. yad --progress --text="Converting to $3..." --width=300 --no-buttons --auto-close
  61. rm $tempname
  62. ;;
  63. esac
  64. }
  65. export -f run_scan # Make function available to yad button command.
  66. # Display scan-dialog.
  67. yad --form --orient=hor --maximized --width=750 --height=250 --columns=3 \
  68. --title="Scanning Options" \
  69. --field="Scan Folder:SFL" /mnt/public \
  70. --field="Filename Prefix:SFL" scan \
  71. --field="File format:CB" jpg\!png\!pdf \
  72. --field="Paper Size:CB" A4\!A5 \
  73. --field="Resolution (DPI):CB" 75\!^150\!300\!600\!1200 \
  74. --field="Scan Buttons:BTN" "" \
  75. --field="Colour:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 Color"' \
  76. --field="Grayscale:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 Gray"' \
  77. --field="B&W:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 Lineart"' \
  78. --field="...:BTN" "" \
  79. --field="Presets:BTN" "" \
  80. --field="Art (Colour PNG):FBTN" 'bash -c "run_scan %1 art png %4 %5 Color"' \
  81. --field="Art (Colour JPG):FBTN" 'bash -c "run_scan %1 art jpg %4 %5 Color"' \
  82. --field="Doc (Gray PDF):FBTN" 'bash -c "run_scan %1 doc pdf %4 %5 Gray"' \
  83. --field="Doc (B&W PDF):FBTN" 'bash -c "run_scan %1 doc pdf %4 %5 Lineart"' \
  84. --no-buttons --buttons-layout=center