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.

171 lines
5.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. INPUT_MTIME=$(stat -c "%Y" "${INPUT}")
  40. OLDFILE_CR2=$(basename "${INPUT}")
  41. # Once we do this, it's very clear which kind of CR2 file we're talking about here.
  42. NEWFILE_CR2=$(basename "${INPUT}" | sed 's/\.\w*$/_dualiso.CR2/')
  43. # Converted Dual ISO file.
  44. NEWFILE_DNG=$(basename "${INPUT}" | sed 's/\.\w*$/_dualiso.DNG/')
  45. # If converted Dual ISO exists already, we skip the conversion process.
  46. # This test passes only if the file doesn't exist.
  47. if [[ ! -e "${INPUT_DIR}/converted_dual_iso/${NEWFILE_DNG}" ]]
  48. then
  49. # Test an input file for Dual ISO.
  50. if [[ $(cr2hdr --dry-run "${INPUT}" | grep "Interlaced ISO detected" | wc -l) -eq 1 ]]
  51. then
  52. echo "Interlaced ISO detected: ${OLDFILE_CR2}"
  53. # Rename detected dual ISO CR2 file with a proper prefix
  54. # (so that we can distinguish Dual ISO images from "normal" CR2 files)
  55. mv "${INPUT_DIR}/${OLDFILE_CR2}" "${INPUT_DIR}/${NEWFILE_CR2}"
  56. # Input we will use from this point is the renamed file,
  57. # so we set INPUT variable to point to the renamed file.
  58. INPUT="${INPUT_DIR}/${NEWFILE_CR2}"
  59. # Process a valid input file.
  60. cr2hdr --process "${INPUT}"
  61. # Move converted dual iso file.
  62. mv "${INPUT_DIR}/${NEWFILE_DNG}" "${INPUT_DIR}/converted_dual_iso/"
  63. # Add Subject=Dual-ISO tag for every Dual ISO CR2 file.
  64. echo "Writing new EXIF/XMP tag Subject: Dual ISO CR2"
  65. exiftool -xmp:subject="Dual ISO CR2" "${INPUT_DIR}/${NEWFILE_CR2}" -overwrite_original
  66. touch --date=@"${INPUT_MTIME}" "${INPUT_DIR}/${NEWFILE_CR2}"
  67. touch --date=@"${INPUT_MTIME}" "${INPUT_DIR}/converted_dual_iso/${NEWFILE_DNG}"
  68. fi
  69. fi
  70. ##############################################
  71. # PROGRESSBAR CODE - BEGIN
  72. let inc++
  73. # Percentage needs to be calculated like this due to bash rounding limitations.
  74. PERCENT_VALUE=$(((${mltp}*${tics})/(200*${numargs}/${inc} % 2 + ${mltp}*${numargs}/${inc})))
  75. # Output: 20, 40, 59, 80, 100 etc.
  76. qdbus $dbusRef Set "" "value" "${PERCENT_VALUE}";
  77. qdbus $dbusRef setLabelText "$LABELTEXT (${inc}/${numargs})";
  78. # PROGRESSBAR CODE - END
  79. # Move to the next file.
  80. shift
  81. done
  82. ##############################################
  83. # Close processing window if cancelled event has been triggered.
  84. # PROGRESSBAR CODE - BEGIN
  85. # If the process was cancelled, remove tmp file and exit the script.
  86. if [[ ! $(qdbus $dbusRef wasCancelled) == "false" ]]
  87. then
  88. exit 0
  89. fi
  90. # Close processing window if not cancelled and processing finished.
  91. qdbus $dbusRef close
  92. # PROGRESSBAR CODE - END
  93. ##############################################
  94. if [[ $(pgrep -x 'cr2hdr' | wc -l) == 0 ]]
  95. then
  96. notify-send 'Dual ISO' -i image-x-krita 'Conversion done!'
  97. fi
  98. ############################################################################################
  99. QUESTCOUNT=0 #Ask this question only once
  100. # Dual ISO (Subject only defined in converted Dual ISO DNG images)
  101. for i in $(find "${INPUT_DIR}" -maxdepth 1 -type f -iname "*.DNG")
  102. do
  103. if \
  104. [[ ! -z $(echo -n "${i}") ]] && \
  105. [[ $(exiftool "${i}" |grep --max-count=1 "Subject" | sed -e 's/.*: //g') == *"Dual-ISO"* ]]
  106. then
  107. if [[ "${QUESTCOUNT}" -eq 0 ]]; then
  108. 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?";)
  109. echo "${QUESTION}"
  110. MOVEALL=true
  111. let QUESTCOUNT++
  112. else
  113. MOVEALL=false
  114. fi
  115. if [[ "${MOVEALL}" == true ]]; then
  116. mv "${i}" "${INPUT_DIR}/converted_dual_iso/"
  117. echo "DEBUG: all detected Dual ISO images moved to 'converted_dual_iso' folder"
  118. fi
  119. fi
  120. done
  121. ############################################################################################
  122. # If there are no files converted, we delete converted_dual_iso folder
  123. if [[ $(ls "${INPUT_DIR}/converted_dual_iso" | wc -l) -eq 0 ]]
  124. then
  125. rm -Rf "${INPUT_DIR}/converted_dual_iso"
  126. fi
  127. exit 0