postprocess.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/sh
  2. # The post-processing script gets called after taking a burst of
  3. # pictures into a temporary directory. The first argument is the
  4. # directory containing the raw files in the burst. The contents
  5. # are 1.dng, 2.dng.... up to the number of photos in the burst.
  6. #
  7. # The second argument is the filename for the final photo without
  8. # the extension, like "/home/user/Pictures/IMG202104031234"
  9. #
  10. # The third argument is 1 or 0 for the cleanup user config. If this
  11. # is 0 the .dng file should not be moved to the output directory
  12. #
  13. # The post-processing script is responsible for cleaning up
  14. # temporary directory for the burst.
  15. set -e
  16. if [ "$#" -ne 3 ]; then
  17. echo "Usage: $0 [burst-dir] [target-name] [save-dng]"
  18. exit 2
  19. fi
  20. BURST_DIR="$1"
  21. TARGET_NAME="$2"
  22. SAVE_DNG="$3"
  23. MAIN_PICTURE="$BURST_DIR"/1
  24. # Copy the first frame of the burst as the raw photo
  25. cp "$BURST_DIR"/1.dng "$TARGET_NAME.dng"
  26. # Create a .jpg if raw processing tools are installed
  27. DCRAW=""
  28. TIFF_EXT="dng.tiff"
  29. if command -v "dcraw_emu" > /dev/null
  30. then
  31. DCRAW=dcraw_emu
  32. # -fbdd 1 Raw denoising with FBDD
  33. set -- -fbdd 1
  34. elif [ -x "/usr/lib/libraw/dcraw_emu" ]; then
  35. DCRAW=/usr/lib/libraw/dcraw_emu
  36. # -fbdd 1 Raw denoising with FBDD
  37. set -- -fbdd 1
  38. elif command -v "dcraw" > /dev/null
  39. then
  40. DCRAW=dcraw
  41. TIFF_EXT="tiff"
  42. set --
  43. fi
  44. CONVERT=""
  45. if command -v "convert" > /dev/null
  46. then
  47. CONVERT="convert"
  48. # -fbdd 1 Raw denoising with FBDD
  49. set -- -fbdd 1
  50. elif command -v "gm" > /dev/null
  51. then
  52. CONVERT="gm"
  53. fi
  54. if [ -n "$DCRAW" ]; then
  55. # +M use embedded color matrix
  56. # -H 4 Recover highlights by rebuilding them
  57. # -o 1 Output in sRGB colorspace
  58. # -q 3 Debayer with AHD algorithm
  59. # -T Output TIFF
  60. $DCRAW +M -H 4 -o 1 -q 3 -T "$@" "$MAIN_PICTURE.dng"
  61. # If imagemagick is available, convert the tiff to jpeg and apply slight sharpening
  62. if [ -n "$CONVERT" ];
  63. then
  64. if [ "$CONVERT" = "convert" ]; then
  65. convert "$MAIN_PICTURE.$TIFF_EXT" -sharpen 0x1.0 -sigmoidal-contrast 6,50% "$TARGET_NAME.jpg"
  66. else
  67. # sadly sigmoidal contrast is not available in imagemagick
  68. gm convert "$MAIN_PICTURE.$TIFF_EXT" -sharpen 0x1.0 "$TARGET_NAME.jpg"
  69. fi
  70. # If exiftool is installed copy the exif data over from the tiff to the jpeg
  71. # since imagemagick is stupid
  72. if command -v exiftool > /dev/null
  73. then
  74. exiftool -tagsFromfile "$MAIN_PICTURE.$TIFF_EXT" \
  75. -software="Megapixels" \
  76. -overwrite_original "$TARGET_NAME.jpg"
  77. fi
  78. echo "$TARGET_NAME.jpg"
  79. else
  80. cp "$MAIN_PICTURE.$TIFF_EXT" "$TARGET_NAME.tiff"
  81. echo "$TARGET_NAME.tiff"
  82. fi
  83. fi
  84. # Clean up the temp dir containing the burst
  85. rm -rf "$BURST_DIR"
  86. # Clean up the .dng if the user didn't want it
  87. if [ "$SAVE_DNG" -eq "0" ]; then
  88. rm "$TARGET_NAME.dng"
  89. fi