Create Canon DSLR CR2 image statistics (exiftool & GNU Plot) and convert ML dual ISO CR2 files painlessly for post-processing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/bin/env bash
  2. # Batch convert multiple Magic Lantern dual ISO image files on Linux
  3. # Copyright (C) 2017,2023 Pekka Helenius
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. #
  19. ###############################################
  20. # We get the directory just from the first filename.
  21. INPUT_DIR=$(dirname "${1}")
  22. DIR_BASENAME=$(echo "${INPUT_DIR}" | rev | cut -d/ -f 1 | rev)
  23. mkdir -p "${INPUT_DIR}/converted_dual_iso"
  24. ############################################################################################
  25. # PROGRESSBAR CODE - BEGIN
  26. LABELTEXT="Processing RAW images..."
  27. numargs=$# # Number of all files
  28. tics=100 # Percentage tics
  29. inc=0 # Current file number
  30. mltp=1000 # Percentage multiplier for bash
  31. dbusRef=$(kdialog --title "Dual ISO (folder: ${DIR_BASENAME})" --progressbar "${LABELTEXT}" "${tics}")
  32. qdbus $dbusRef showCancelButton true
  33. # PROGRESSBAR CODE - END
  34. while \
  35. [[ $# -gt 0 ]] && \
  36. [[ $(qdbus $dbusRef wasCancelled) == "false" ]]
  37. do
  38. INPUT="${1}"
  39. OLDFILE_CR2=$(basename "${INPUT}")
  40. # Once we do this, it's very clear which kind of CR2 file we're talking about here.
  41. NEWFILE_CR2=$(basename "${INPUT}" | sed 's/\.\w*$/_dualiso.CR2/')
  42. # Converted Dual ISO file.
  43. NEWFILE_DNG=$(basename "${INPUT}" | sed 's/\.\w*$/_dualiso.DNG/')
  44. # If converted Dual ISO exists already, we skip the conversion process.
  45. # This test passes only if the file doesn't exist.
  46. if [[ ! -e "${INPUT_DIR}/converted_dual_iso/${NEWFILE_DNG}" ]]
  47. then
  48. # Test an input file for Dual ISO.
  49. if [[ $(cr2hdr --dry-run "${INPUT}" | grep "Interlaced ISO detected" | wc -l) -eq 1 ]]
  50. then
  51. echo "Interlaced ISO detected: ${OLDFILE_CR2}"
  52. # Rename detected dual ISO CR2 file with a proper prefix
  53. # (so that we can distinguish Dual ISO images from "normal" CR2 files)
  54. mv "${INPUT_DIR}/${OLDFILE_CR2}" "${INPUT_DIR}/${NEWFILE_CR2}"
  55. # Input we will use from this point is the renamed file,
  56. # so we set INPUT variable to point to the renamed file.
  57. INPUT="${INPUT_DIR}/${NEWFILE_CR2}"
  58. # Process a valid input file.
  59. cr2hdr --process "${INPUT}"
  60. # Move converted dual iso file.
  61. mv "${INPUT_DIR}/${NEWFILE_DNG}" "${INPUT_DIR}/converted_dual_iso/"
  62. # Add Subject=Dual-ISO tag for every Dual ISO CR2 file.
  63. echo "Writing new EXIF/XMP tag Subject: Dual ISO CR2"
  64. exiftool -xmp:subject="Dual ISO CR2" "${INPUT_DIR}/${NEWFILE_CR2}" -overwrite_original
  65. fi
  66. fi
  67. ##############################################
  68. # PROGRESSBAR CODE - BEGIN
  69. let inc++
  70. # Percentage needs to be calculated like this due to bash rounding limitations.
  71. PERCENT_VALUE=$(((${mltp}*${tics})/(200*${numargs}/${inc} % 2 + ${mltp}*${numargs}/${inc})))
  72. # Output: 20, 40, 59, 80, 100 etc.
  73. qdbus $dbusRef Set "" "value" "${PERCENT_VALUE}";
  74. qdbus $dbusRef setLabelText "$LABELTEXT (${inc}/${numargs})";
  75. # PROGRESSBAR CODE - END
  76. # Move to the next file.
  77. shift
  78. done
  79. ##############################################
  80. # Close processing window if cancelled event has been triggered.
  81. # PROGRESSBAR CODE - BEGIN
  82. # If the process was cancelled, remove tmp file and exit the script.
  83. if [[ ! $(qdbus $dbusRef wasCancelled) == "false" ]]
  84. then
  85. exit 0
  86. fi
  87. # Close processing window if not cancelled and processing finished.
  88. qdbus $dbusRef close
  89. # PROGRESSBAR CODE - END
  90. ##############################################
  91. if [[ $(pgrep -x 'cr2hdr' | wc -l) == 0 ]]
  92. then
  93. notify-send 'Dual ISO' -i image-x-krita 'Conversion done!'
  94. fi
  95. ############################################################################################
  96. QUESTCOUNT=0 #Ask this question only once
  97. # Dual ISO (Subject only defined in converted Dual ISO DNG images)
  98. for i in $(find "${INPUT_DIR}" -maxdepth 1 -type f -iname "*.DNG")
  99. do
  100. if \
  101. [[ ! -z $(echo -n "${i}") ]] && \
  102. [[ $(exiftool "${i}" |grep --max-count=1 "Subject" | sed -e 's/.*: //g') == *"Dual-ISO"* ]]
  103. then
  104. if [[ "${QUESTCOUNT}" -eq 0 ]]; then
  105. QUESTION=$(kdialog --yesno "More Dual ISO files detected in '$(echo ${INPUT_DIR} | rev | cut -d/ -f1 | rev)' main folder. Do you want to move these files into 'converted_dual_iso' folder?";)
  106. echo "${QUESTION}"
  107. MOVEALL=true
  108. let QUESTCOUNT++
  109. else
  110. MOVEALL=false
  111. fi
  112. if [[ "${MOVEALL}" == true ]]; then
  113. mv "${i}" "${INPUT_DIR}/converted_dual_iso/"
  114. echo "DEBUG: all detected Dual ISO images moved to 'converted_dual_iso' folder"
  115. fi
  116. fi
  117. done
  118. ############################################################################################
  119. # If there are no files converted, we delete converted_dual_iso folder
  120. if [[ $(ls "${INPUT_DIR}/converted_dual_iso" | wc -l) -eq 0 ]]
  121. then
  122. rm -Rf "${INPUT_DIR}/converted_dual_iso"
  123. fi
  124. exit 0