User Tools

Site Tools


en:linux:scripts:makepano

/home/<username>/bin/makepano

Script to create panoramatic pictures in a more automatic manner.

#!/bin/bash

# helpful web: http://wiki.panotools.org/Panorama_scripting_in_a_nutshell

#----------------------------------------------------------------------------------------------------

logFile="makepano.log"
autoPtoFile="automatic.pto"
autoPtoFileBkp="automatic_backup.pto"
manualPtoFile="manual.pto"

#----------------------------------------------------------------------------------------------------

function PrintUsage()
{
	echo "USAGE: makepano <commands>"
	echo "COMMANDS:"
	echo -e "\thelp, -h, --help\tprint this help"
	echo -e "\tprepare\t\t\tprepares control points and performs default optimization"
	echo -e "\tset_preview\t\tsets preview size"
	echo -e "\tset_final\t\tsets final (optimal) size"
	echo -e "\tstitch\t\t\truns nona to stich the panorama"
	echo -e "\tenblend\t\t\truns enblend"
	echo -e "\tfinish\t\t\tnot yet implemented"
	echo -e "\tmanual\t\t\tuse this if you decide to make the panorama manually with Hugin"

	exit 0
}

#----------------------------------------------------------------------------------------------------

function DoPrepareCommand()
{
	# get inputs, prune preview, final and manual.jpg
	input=(`ls -1|grep -e '.\(jpg\|JPG\|jpeg\)'|grep -ve '\(preview\|final\|manual\).jpg'`)
	N=${#input[*]}
	fixed=$(( ($N - 1) / 2 ))

	#echo ${input[*]}
	#return
	
	# generate control points
	autopano-sift-c --projection 2,50 --maxmatches 20 "$autoPtoFile" ${input[*]}
	
	# clean control points
	celeste_standalone -i "$autoPtoFile" -o "$autoPtoFile"

	# remove default optimization modes
	cp "$autoPtoFile" "_$autoPtoFile"
	cat "_$autoPtoFile" | grep -ve '^v' > "$autoPtoFile"
	rm "_$autoPtoFile"

	# add standard optimization modes
	for ((a=0; a < N ; a++))
	do
		if [ ! $fixed -eq $a ]
		then
			echo "v y$a p$a r$a" >> "$autoPtoFile"
		fi
	done

	# topology optimisation
	autooptimiser -n -l -o "$autoPtoFile" "$autoPtoFile"
	echo "v a0 b0" >> "$autoPtoFile"
	autooptimiser -n -l -s -o "$autoPtoFile" "$autoPtoFile"
	
	# photometric optimisation - skipped for the time being
}

#----------------------------------------------------------------------------------------------------

function ProcessDirectory()
{
	echo "> processing directory '$1'"

	# do not logged action first

	#--- MANUAL ---
	if [ $cManual -eq 1 ]
	then
		echo -e "\tmanual"
		cp "$autoPtoFile" "$autoPtoFileBkp"
		mv "$autoPtoFile" "$manualPtoFile"
		return
	fi


	# start log entry
	echo "----------------------" >> "$logFile"
	date >> "$logFile"
	echo "----------------------" >> "$logFile"
	echo "> processing directory '$1'" >> "$logFile"

	# do logged actions now

	#--- PREPARE ---
	if [ $cPrepare -eq 1 ]
	then
		echo -e "\tprepare"
		echo "> running prepare command" >> "$logFile"
		DoPrepareCommand &>> "$logFile"
	fi

	#--- STITCH ---
	if [ $cStitch -eq 1 ]
	then
		echo -e "\tstitch"
		echo "> running stitch command" >> "$logFile"

		ptoFile=""
		outputPrefix=""
		if [ -s "$autoPtoFile" ]; then ptoFile="$autoPtoFile"; outputPrefix="preview"; fi
		if [ -s "$manualPtoFile" ]; then ptoFile="$manualPtoFile"; outputPrefix="manual"; fi

		if [ -n "$ptoFile" ]
		then
			nona -m TIFF_m -o "$outputPrefix" "$ptoFile" &>> "$logFile"
		else
			echo -e "\t\tNo .pto file found! Try running prepare command."
			echo -e "ERROR: No .pto file found! Try running prepare command." >> "$logFile"
		fi
	fi

	#--- ENBLEND ---
	if [ $cEnblend -eq 1 ]
	then
		echo -e "\tenblend"
		echo "> running enblend command" >> "$logFile"
		outFile=""
		if [ -s "$autoPtoFile" ]; then outFile="preview.tif"; fi
		if [ -s "$manualPtoFile" ]; then outFile="manual.tif"; fi
		enblend -o "$outFile" *.tif &>> "$logFile"
	fi
	
	#--- FINISH ---
	if [ $cFinish -eq 1 ]
	then
		echo -e "\tfinish"
		echo "> finishing" >> "$logFile"
		inFile=""
		if [ -s "preview.tif" ]; then inFile="preview.tif"; fi
		if [ -s "manual.tif" ]; then inFile="manual.tif"; fi
		if [ -z "$inFile" ]
		then
			echo -e "\t\tNo file to finalize! Try running stitch command."
			echo -e "ERROR: No file to finalize! Try running stitch command." >> "$logFile"
		fi

		outFile="../$1.jpg"
		input=(`ls -1|grep -e '.\(jpg\|JPG\|jpeg\)'|grep -ve '\(preview\|final\|manual\).jpg'`)
		exifFile=${input[0]}
		convert "$inFile" "$outFile" &>> "$logFile"
		jhead -te "$exifFile" "$outFile" &>> "$logFile"
		jhead -ft "$outFile" &>> "$logFile"
	fi
}

#----------------------------------------------------------------------------------------------------
# MAIN

# no commands make no sense
if [ -z "$1" ]
then
	PrintUsage
fi

# command flags
cPrepare=0
cPreviewSize=0
cFinalSize=0
cStitch=0
cEnblend=0
cFinish=0
cManual=0

# process command line
while [ -n "$1" ]
do
	if [ "$1" == "-h" -o "$1" == "--help" -o "$1" == "help" ]; then PrintUsage; fi

	if [ "$1" == "prepare" ]; then cPrepare=1; shift; continue; fi
	if [ "$1" == "set_preview" ]; then cPreviewSize=1; shift; continue; fi
	if [ "$1" == "set_final" ]; then cFinalSize=1; shift; continue; fi
	if [ "$1" == "stitch" ]; then cStitch=1; shift; continue; fi
	if [ "$1" == "enblend" ]; then cEnblend=1; shift; continue; fi
	if [ "$1" == "finish" ]; then cFinish=1; shift; continue; fi
	if [ "$1" == "manual" ]; then cManual=1; shift; continue; fi

	# default
	echo -e "Unrecognized command: $1\n"
	PrintUsage
done

# check whether in a directory with jpg files -> process only this directory
if [ "$(find . -type d)" == "." ]
then
	dir=`pwd`
	dir=${dir##*/}
	ProcessDirectory "$dir"
else
	# process all sub-directories or current directory
	ls -1|while read dir
	do
		if [ -d "$dir" ]
		then
			cwd=`pwd`
			cd "$dir" && ProcessDirectory "$dir"
			cd "$cwd"
		fi
	done
fi
en/linux/scripts/makepano.txt · Last modified: 2010/09/01 11:02 (external edit)