postprocess.sh 3.0 KB

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