scan-dialog 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/env bash
  2. export GDK_BACKEND=x11
  3. # Specify device to avoid startup delays and scanning slowness.
  4. # scanimage --list-devices
  5. export SANE_DEFAULT_DEVICE='epsonds:libusb:001:005'
  6. # See if file exists, add a number to name if needed.
  7. function check_filename {
  8. path=$1
  9. folder=$(dirname "$path")
  10. file=$(basename "$path")
  11. extension="${file##*.}"
  12. filename="${file%.*}"
  13. if [[ -e "$path" ]]; then
  14. i=2
  15. while [[ -e "$folder/$filename-$i.$extension" ]]; do
  16. let i++
  17. done
  18. filename=$filename-$i
  19. fi
  20. echo "$folder/$filename.$extension"
  21. }
  22. export -f check_filename # Make available to run_scan's command substitution
  23. # Run scanner. arguments:
  24. # 1-folder 2-prefix 3-timestamp 4-format 5-papersize
  25. # 6-resolution 7-iccprofile 8-colourmode
  26. function run_scan {
  27. format=$4
  28. res=$6
  29. colmode=$8
  30. icc=$7
  31. # Paper sizes.
  32. landscape=false
  33. case $5 in
  34. A4)
  35. x=210 y=297
  36. ;;
  37. A4-R)
  38. x=210 y=297 landscape=true
  39. ;;
  40. A5)
  41. x=148 y=210
  42. ;;
  43. A5-R)
  44. x=210 y=148
  45. # Force A4 instead, and cut later, if 150DPI..
  46. # Sane epson backend throws an out of memory error at ~A5R 150DPI.
  47. if [[ $res == "150" ]]; then
  48. x=210 y=297
  49. fi
  50. ;;
  51. B5)
  52. x=176 y=250
  53. ;;
  54. B5-R)
  55. x=176 y=250 landscape=true
  56. ;;
  57. LTR)
  58. x=216 y=279
  59. ;;
  60. LTR-R)
  61. x=216 y=279 landscape=true
  62. ;;
  63. esac
  64. filename=$1/$2-$(date +"%Y-%m-%d-%H%M%S").$format
  65. filename=$(check_filename $filename)
  66. userid=$(id -u $USER)
  67. tempname="/var/run/user/$userid/temp-$(date +%s).tif"
  68. echo "$colmode $res $x $y $filename"
  69. scanimage --mode=$colmode --resolution=$res -x $x -y $y --format=tiff --output-file=$tempname | \
  70. yad --progress --text="Scanning (TIFF)..." --width=300 --no-buttons --auto-close
  71. options=""
  72. [[ $landscape == true ]] && options="${options} -rotate 90"
  73. [[ $icc == "Yes" ]] && options="${options} -profile /home/cinaeco/scan-dialog/scanner.icm"
  74. echo "Options: $options"
  75. convert $options $tempname $tempname | \
  76. yad --progress --text="Modifying..." --width=300 --no-buttons --auto-close
  77. case $format in
  78. png)
  79. convert $tempname $filename | \
  80. yad --progress --text="Converting to $format..." --width=300 --no-buttons --auto-close
  81. ;;
  82. jpg)
  83. convert -quality 85 $tempname $filename | \
  84. yad --progress --text="Converting to $format..." --width=300 --no-buttons --auto-close
  85. ;;
  86. pdf)
  87. if [[ $colmode != "Lineart" ]]; then
  88. tempname2="${tempname%.png}.jpg"
  89. convert -quality 85 $tempname $tempname2 | \
  90. yad --progress --text="Converting to jpg..." --width=300 --no-buttons --auto-close
  91. rm $tempname
  92. tempname=$tempname2
  93. fi
  94. convert $tempname $filename | \
  95. yad --progress --text="Converting to $format..." --width=300 --no-buttons --auto-close
  96. ;;
  97. tiff)
  98. cp $tempname $filename
  99. ;;
  100. esac
  101. size_raw=$(stat -c%s $filename)
  102. size_kb=$((size_raw/1024))
  103. yad --picture --title="Filesize: $size_kb KB ($format)" --size=fit --width=750 --height=1000 --filename=$tempname
  104. rm $tempname
  105. }
  106. export -f run_scan # Make function available to yad button command.
  107. # Display scan-dialog.
  108. yad --form --orient=hor --maximized --width=750 --height=250 --columns=3 --focus-field=5 \
  109. --title="Scanning Options" \
  110. --field="Scan Folder:SFL" /mnt/public/scans \
  111. --field="Filename Prefix:SFL" scan \
  112. --field="Timestamp:CB" Yes\!No \
  113. --field="File format:CB" jpg\!png\!pdf\!tiff \
  114. --field="Paper Size:CB" A4\!A4-R\!A5\!A5-R\!B5\!B5-R\!LTR\!LTR-R\! \
  115. --field="Resolution (DPI):CB" 75\!150\!^300\!600\!1200 \
  116. --field="Apply ICC Profile:CB" Yes\!No \
  117. --field="Scan Buttons:BTN" "" \
  118. --field="Colour:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 %6 %7 Color"' \
  119. --field="Grayscale:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 %6 %7 Gray"' \
  120. --field="B&W:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 %6 %7 Lineart"' \
  121. --field="...:BTN" "" \
  122. --field="...:BTN" "" \
  123. --field="...:BTN" "" \
  124. --field="Presets:BTN" "" \
  125. --field="Art (Colour PNG):FBTN" 'bash -c "run_scan %1 art %3 png %5 %6 %7 Color"' \
  126. --field="Art (Colour JPG):FBTN" 'bash -c "run_scan %1 art %3 jpg %5 %6 %7 Color"' \
  127. --field="Doc (Colour PDF):FBTN" 'bash -c "run_scan %1 doc %3 pdf %5 %6 %7 Color"' \
  128. --field="Doc (Gray PDF):FBTN" 'bash -c "run_scan %1 doc %3 pdf %5 %6 %7 Gray"' \
  129. --field="Doc (B&W PDF):FBTN" 'bash -c "run_scan %1 doc %3 pdf %5 %6 %7 Lineart"' \
  130. --field="...:BTN" "" \
  131. --no-buttons --buttons-layout=center