Skip to content

Instantly share code, notes, and snippets.

@bfontaine
Last active July 4, 2020 16:42
Show Gist options
  • Save bfontaine/dcb19bfe1cb168b8dd65125f422b2b8f to your computer and use it in GitHub Desktop.
Save bfontaine/dcb19bfe1cb168b8dd65125f422b2b8f to your computer and use it in GitHub Desktop.
#! /bin/bash -e
# https://wiki.panotools.org/Panorama_scripting_in_a_nutshell
# http://hugin.sourceforge.net/tutorials/scans/en.shtml
# This is a tool to vertically stitch together screenshots of black and white
# scans.
#
# Install:
#
# brew cask install hugin
#
# Usage:
#
# ./stitch.sh *.png
BIN="/Applications/Hugin/tools_mac"
TMP="$(mktemp -d)"
PROJECT=p.pto
PREFIX=stitch
(
IFS=
for img in $* ; do
cp "$img" "$TMP/"
done
)
pushd "$TMP"
echo "=> creating project"
echo $@
"$BIN/pto_gen" -o "$PROJECT" --projection=0 --fov=10 *.png
echo "=> generating control points"
"$BIN/cpfind" --multirow -o "$PROJECT" "$PROJECT"
echo "=> cleaning"
"$BIN/cpclean" -o "$PROJECT" "$PROJECT"
echo "=> optimizing"
"$BIN/linefind" -o "$PROJECT" "$PROJECT"
"$BIN/autooptimiser" -a -l -s -m -o "$PROJECT" "$PROJECT"
"$BIN/autooptimiser" -m -o "$PROJECT" "$PROJECT"
echo "=> setting up the output"
# https://manpages.debian.org/testing/hugin-tools/pano_modify.1.en.html
"$BIN/pano_modify" -o "$PROJECT" --projection=0 --fov=AUTO --center \
--canvas=AUTO "$PROJECT"
echo "=> stitching"
"$BIN/hugin_executor" --stitching "--prefix=$PREFIX" "$PROJECT"
echo "=> trimming"
# remove '-colorspace gray' to preserve color
convert "$PREFIX".tif -trim -colorspace gray "$PREFIX".png
if [ x"$(which optipng)" != x ]; then
echo "=> optimizing the output"
optipng -o1 "$PREFIX".png
fi
popd
mv "$TMP/$PREFIX.png" ./
rm -rf "$TMP"
echo "Done: $PREFIX.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment