Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Composite photo developer (require dcraw & ImageMagick)
#!/bin/bash
set -e
function genfn {
printf "s%02d-%04d.ppm" $1 $2;
}
function print_usage {
echo "[Usage] $0 dark-frame [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
}
DARK_FRAME=$1
if [[ $2 != "" ]]; then
DCRAW_OPT=$2
else
DCRAW_OPT="-vf4 -r 1.783446 1.000000 1.997113 1 -b 0.08 -n 100"
fi
STAGE_NUM=0
NUM1=0
NUM2=0
echo -e "¥033[0;35m#### Composite photo developer ####"
echo -e "¥033[0;33mDark-frame filename: ¥033[0m${DARK_FRAME}"
echo -e "¥033[0;33mdcraw option: ¥033[0m${DCRAW_OPT}"
echo ""
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
FILENUM=$(($(find . -name "*.NEF"|wc -l)-1))
if (( $FILENUM & ($FILENUM - 1) )); then
print_error "Number of files is not a power of 2."
fi
unset FILENUM
print_header "Develop RAW to 16bit ppm"
dcraw ${DCRAW_OPT} *.NEF 2>&1 | /usr/bin/grep NEF
mv ${1%.*}.ppm df.ppm
echo "done."
print_header "Dark-frame subtraction"
for f in $(find . -name "_DSC*.ppm"); do
FN1=$(genfn ${STAGE_NUM} ${NUM1})
echo " ${f##*/} - df.ppm -> ${FN1}"
composite -compose minus $f df.ppm ${FN1}
NUM1=$((NUM1+1))
done
NUM1=0
echo "done."
print_header "Tournament composite"
while [[ $(find . -name "s$(printf "%02d" ${STAGE_NUM})-*.ppm" | wc -l) -ne 1 ]]
do
PREV_STAGE=${STAGE_NUM}
STAGE_NUM=$((STAGE_NUM+1))
echo -e "¥033[0;34m===> ¥033[0;35mStage ${STAGE_NUM}¥033[0m"
while [[ -e $(genfn ${PREV_STAGE} ${NUM1}) ]]; do
FN1=$(genfn ${PREV_STAGE} ${NUM1})
FN2=$(genfn ${PREV_STAGE} $((NUM1+1)))
FNN=$(genfn ${STAGE_NUM} ${NUM2})
echo " ${FN1} + ${FN2} -> ${FNN}"
#composite -compose plus ${FN1} ${FN2} ${FNN}
composite -compose lighten ${FN1} ${FN2} ${FNN}
NUM1=$((NUM1+2))
NUM2=$((NUM2+1))
done
NUM1=0
NUM2=0
echo " done."
done
echo "done."
print_header "Normalize"
echo "convert -normalize $(genfn ${STAGE_NUM} 0) composite.ppm"
convert -normalize $(genfn ${STAGE_NUM} 0) composite.ppm
echo "done."
print_header "Convert to JPEG"
echo "convert composite.ppm composite.jpg"
convert composite.ppm composite.jpg
echo "done."
print_header "Cleaning"
echo "rm -f *.ppm"
rm -f *.ppm
echo "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment