Skip to content

Instantly share code, notes, and snippets.

@frah
Created October 11, 2012 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frah/3874231 to your computer and use it in GitHub Desktop.
Save frah/3874231 to your computer and use it in GitHub Desktop.
Composite photo developer (require dcraw & ImageMagick)
#!/bin/bash
set -e
shopt -s nocasematch
function print_usage {
echo "[Usage] $0 input-files dark-frame output-filename [dcraw_option]"
}
function print_header {
echo -e "\033[0;34m==> \033[0;35m$1\033[0m"
}
function print_error {
echo -e "\033[0;31mError: \033[0m$1"
print_usage
exit 1
}
function get_exif {
exiftool -$2 $1 | sed -e 's/^.*: //'
}
INPUT_FILES=$1
DARK_FRAME=$2
OUTPUT_FILE=$3
if [[ $4 != "" ]]; then
DCRAW_OPT=$4
else
DCRAW_OPT="-fw4"
fi
STAGE_NUM=0
NUM1=0
NUM2=0
echo -e "\033[0;35m#### Composite photo developer ####"
echo -e "\033[0;33mInput files: \033[0m${INPUT_FILES}"
echo -e "\033[0;33mDark-frame filename: \033[0m${DARK_FRAME}"
echo -e "\033[0;33mOutput filename: \033[0m${OUTPUT_FILE}"
echo -e "\033[0;33mdcraw option: \033[0m${DCRAW_OPT}"
echo ""
FILE_NUM=$(find . -name "${INPUT_FILES}" | wc -l | tr -d ' ')
if [[ ${INPUT_FILES} == "" ]]; then
print_error "No input files."
elif [[ ${FILE_NUM} -eq 0 ]]; then
print_error "Input files are not found."
elif [[ ! ${INPUT_FILES##*.} =~ (nef|crw|cr2|arw) ]]; then
print_error "The specified file type is not corresponded."
fi
if [[ ${DARK_FRAME} == "" ]]; then
print_error "Dark-frame file is not specified."
elif [[ ! -f ${DARK_FRAME} ]]; then
print_error "Dark-frame file is not found."
fi
if [[ ${OUTPUT_FILE} == "" ]]; then
print_error "Output filename is not specified."
elif [[ -f ${OUTPUT_FILE} ]]; then
echo -n "${OUTPUT_FILE} is already exists. Overwrite it? [y/N]: "
read confirm
if [[ ! "$confirm" =~ ^y(es)?$ ]]; then
print_usage
exit 0
fi
unset confirm
fi
print_header "Develop Dark-frame RAW to 16bit ppm"
dcraw ${DCRAW_OPT} -v ${DARK_FRAME} 2>&1 | /usr/bin/grep NEF
mv ${DARK_FRAME%.*}.ppm df.ppm
echo "done."
print_header "Dark-frame subtraction & composite"
PROG_NUM=0
for f in $(find . -name "${INPUT_FILES}"); do
PROG_NUM=$((PROG_NUM+1))
echo -n " (${PROG_NUM}/${FILE_NUM})${f##*/} In progress..."
if [[ ! -f ${OUTPUT_FILE%.*}.ppm ]]; then
dcraw ${DCRAW_OPT} -c $f \
| composite -compose minus ppm:- df.ppm ${OUTPUT_FILE%.*}.ppm
else
dcraw ${DCRAW_OPT} -c $f \
| composite -compose minus ppm:fd:0 df.ppm ppm:fd:1 \
| composite -compose lighten ppm:fd:0 ${OUTPUT_FILE%.*}.ppm ${OUTPUT_FILE%.*}.ppm
fi
echo "done"
done
echo "done."
print_header "Convert to output file"
echo "convert -normalize -contrast -sharpen 0.5 -noise 2 ${OUTPUT_FILE%.*}.ppm ${OUTPUT_FILE}"
convert -normalize -contrast -sharpen 0.5 -noise 2 ${OUTPUT_FILE%.*}.ppm ${OUTPUT_FILE}
echo "done."
print_header "Update Exif"
for t in "ISO" "ExposureTime" "FNumber" "FocalLength" "Orientation" "LensIDNumber" "LensFStops" "LensType" "ExposureProgram" "Flash" "Aperture" "ShutterSpeed"
do
exiftool -m −overwrite_original -$t="$(get_exif ${DARK_FRAME} $t)" ${OUTPUT_FILE}
done
#exiftool -m −overwrite_original −tagsFromFile ${DARK_FRAME} ${OUTPUT_FILE}
echo "done."
print_header "Cleaning"
echo "rm -f df.ppm"
rm -f df.ppm
echo "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment