トップ «前の日記(2020年05月23日) 最新 次の日記(2020年08月01日)» 編集

Masa's blog

検索キーワード:

2020年06月04日 Shell script to convert nega film to posi [長年日記]

_ nega2posi.sh - ネガフィルム画像をポジ画像に変換するシェルスクリプト(のつもり)

#! /bin/sh
trap "rm /tmp/nega2posi.$$.*.tmp.*; exit 1" INT TERM
#
if [ "X$1" = "X-h" -o "X$1" = "X--help" ]
then
	echo "Usage: $0 [-b X,Y] [-aw | -w X,Y] [-i input] [-o output]"
	echo "Convert input(nega film) to output(posi)."
        echo ""
	echo "-b X,Y              Specify position of black."
	echo "-aw                 Adjust white balance automatically."
	echo "-w X,Y              Specify position of white and adjust white balance."
	echo "-i input            If this option is omitted or - is specified, stdin is assumed."
	echo "-o output           If this option is omitted or - is specified, stdout is assumed."
        exit 0
fi
#
AutoWhiteSw="n"
white=""
black=""
input="-"
output="-"
#
while [ $# -ne 0 ]
do
	case $1 in
	-aw)
		AutoWhiteSw="y"
		;;
	-w)
		shift
		white=`echo $1 |\
		       tr ',' '+'`
		;;
	-b)
		shift
		black=`echo $1 |\
		       tr ',' '+'`
		;;
	-i)
		shift
		input="$1"
		;;
	-o)
		shift
		output="$1"
		;;
	esac
	shift
done
#
if [ "X${black}" = "X" ]
then
	convert ${input} txt: >/tmp/nega2posi.$$.3.tmp.txt
	darkestXYC=`cat /tmp/nega2posi.$$.3.tmp.txt |\
		egrep -v '^#' |\
		tr ',:()' '    ' |\
		tr -s ' ' |\
		awk 'BEGIN{
			FS=" ";
		}
		{
			printf("%d %d %d\n",$1,$2,($3+$4+$4));
		}
		END{
		}' |\
		sort -t ' ' -k 3,3nr |\
		head -1`
	x=`echo ${darkestXYC} | cut -d ' ' -f 1`
	y=`echo ${darkestXYC} | cut -d ' ' -f 2`
	black="${x}+${y}"
fi
#
if [ "X${AutoWhiteSw}" = "Xy" ]
then
	if [ ! -r /tmp/nega2posi.$$.3.tmp.txt ]
	then
		convert ${input} txt: >/tmp/nega2posi.$$.3.tmp.txt
	fi
	brightestXYC=`cat /tmp/nega2posi.$$.3.tmp.txt |\
		egrep -v '^#' |\
		tr ',:()' '    ' |\
		tr -s ' ' |\
		awk 'BEGIN{
			FS=" ";
		}
		{
			printf("%d %d %d\n",$1,$2,($3+$4+$4));
		}
		END{
		}' |\
		sort -t ' ' -k 3,3n |\
		head -1`
	x=`echo ${brightestXYC} | cut -d ' ' -f 1`
	y=`echo ${brightestXYC} | cut -d ' ' -f 2`
	white="${x}+${y}"
fi
#
convert +repage ${input} /tmp/nega2posi.$$.1.tmp.png
size=`convert /tmp/nega2posi.$$.1.tmp.png -format "%wx%h" info:`
convert +repage /tmp/nega2posi.$$.1.tmp.png -crop 1x1+${black} -resize ${size}! -negate -strip /tmp/nega2posi.$$.2.tmp.png
convert +repage /tmp/nega2posi.$$.1.tmp.png /tmp/nega2posi.$$.2.tmp.png \
	-compose blend -composite -negate -equalize /tmp/nega2posi.$$.4.tmp.png
#	-compose blend -composite -negate -auto-level ${output}
#	-compose blend -composite -negate -normalize ${output}
#
if [ "X${white}" = "X" ]
then
	convert +repage /tmp/nega2posi.$$.4.tmp.png ${output}
else
	convert +repage /tmp/nega2posi.$$.4.tmp.png -crop 1x1+${white} -resize ${size}! -negate -strip /tmp/nega2posi.$$.5.tmp.png
	convert +repage /tmp/nega2posi.$$.4.tmp.png /tmp/nega2posi.$$.5.tmp.png \
		-compose blend -composite -equalize ${output}
#		-compose blend -composite -auto-level ${output}
#		-compose blend -composite -normalize ${output}
fi
#
rm /tmp/nega2posi.$$.*.tmp.*