unwarpall 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. set -e
  3. set -u
  4. unwarper=ppmunwarp
  5. unwarp_calib=""
  6. TMPDIR=${TMPDIR:-/tmp}
  7. [[ -f /etc/bookscanner/splitall.rc ]] && source /etc/bookscanner/splitall.rc
  8. usage() {
  9. echo "Usage: $0 DIRECTORY"
  10. echo "The 'first' file is the unwarp-calibration. Every other file will be unwarped"
  11. }
  12. if [[ $# -ne 1 ]]; then
  13. usage >&2
  14. exit 2
  15. fi
  16. dir=$(readlink -f $1)
  17. shift 1
  18. calibrate() {
  19. if [[ ! -f "$dir/unwarp.L" ]]; then
  20. unwarpcalibration=$(ls -1 $dir/*.L.jpg| sort| head -n1)
  21. convert $unwarpcalibration "${unwarpcalibration%.jpg}.ppm"
  22. $unwarper $unwarp_calib -c "${unwarpcalibration%.jpg}.ppm" > "$dir/unwarp.L"
  23. rm "${unwarpcalibration%.jpg}.ppm"
  24. fi
  25. if [[ ! -f "$dir/unwarp.R" ]]; then
  26. unwarpcalibration=$(ls -1 $dir/*.R.jpg| sort| head -n1)
  27. convert $unwarpcalibration "${unwarpcalibration%.jpg}.ppm"
  28. $unwarper $unwarp_calib -c "${unwarpcalibration%.jpg}.ppm" > "$dir/unwarp.R"
  29. rm "${unwarpcalibration%.jpg}.ppm"
  30. fi
  31. }
  32. unwarp() {
  33. [[ $# -eq 1 ]]
  34. imgtounwarp="$1"
  35. unwarped="$dir/unwarp/$(basename $1 .jpg).ppm"
  36. profile="$dir/unwarp.R"
  37. [[ $(basename $imgtounwarp .jpg) =~ .L ]] && profile="$dir/unwarp.L"
  38. [[ -f "$imgtounwarp" ]]
  39. convert $imgtounwarp ppm:- | $unwarper -d "$profile" > "$unwarped"
  40. }
  41. calibrate
  42. mkdir "$dir/unwarp/"
  43. ls -1 $dir/*.jpg | sort | tail -n +3 | while read fname; do
  44. unwarp "$fname"
  45. done
  46. exit 0