scan-dialog 4.9 KB

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