Skip to content

Instantly share code, notes, and snippets.

@anekos
Created February 23, 2020 14:51
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 anekos/60325839781306693614db26d7c87a8f to your computer and use it in GitHub Desktop.
Save anekos/60325839781306693614db26d7c87a8f to your computer and use it in GitHub Desktop.
kakukaku
#!/bin/bash
set -euC
# set -o pipefail
# exec 5>> /tmp/xmosh/shell-script-debug.out
# BASH_XTRACEFD="5"
# PS4='$LINENO: '
# set -x
if [ "$#" -lt 2 ]
then
echo 'kakukaku <SOURCE_IMAGE> <OUTPUT_GIF>'
exit 1
fi
image="$1"
dest="$2"
wd="$(mktemp -d)"
kaku="${wd}/kaku.png"
function cleanup () {
rm -rf "$wd"
}
trap cleanup EXIT
function calc () {
local ex="$*"
echo "$ex" | bc | sed 's/\..*//'
}
command -v caching || function caching () {
"${@}"
}
wh="$(identify -format '%w %h' "$image")"
w="${wh% *}"
h="${wh#* }"
read -r -a reko < <(
caching aws rekognition detect-faces \
--image-bytes "fileb://${image}" |\
jq --raw-output \
'.FaceDetails[0]|.BoundingBox|[.Left,.Top,.Width,.Height]|join(" ")'
)
fx="$( calc "${reko[0]} * ${w}" )"
fy="$( calc "${reko[1]} * ${h}" )"
fw="$( calc "${reko[2]} * ${w}" )"
fh="$( calc "${reko[3]} * ${h}" )"
d="$( calc "$fh * 0.06" )"
convert "$image" \
\( -clone 0 -crop "${fw}x${fh}+${fx}+${fy}" \) \
\( -clone 0-1 -compose Dst -composite \) \
-delete 0 +swap \
-geometry "+$(( fx ))+$(( fy - d ))" -compose Over -composite \
"$kaku"
convert -delay 5 -loop 0 "$image" "$kaku" "$dest"
command -v chrysoberyl && chrysoberyl @@views 2 @@ "$image" "$dest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment