gifcap/gifcap.sh

105 lines
2.4 KiB
Bash
Raw Normal View History

2024-08-08 14:42:14 -06:00
#!/bin/sh
2024-08-08 14:49:18 -06:00
# gifcap: Make an iFunny GIF caption
2024-08-08 14:42:14 -06:00
# Copyright (C) 2024 Sasha Koshka (pseudonymous)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
name="$0"
2024-08-08 20:52:38 -06:00
font="ifunny.otf"
2024-08-08 14:42:14 -06:00
input=""
output=""
caption=""
usage () {
echo "Usage: $name [OPTION]... CAPTION..."
2024-08-08 14:49:18 -06:00
echo "Make an iFunny GIF caption."
2024-08-08 14:42:14 -06:00
echo ""
2024-08-08 15:35:44 -06:00
echo " -f, --font specify a font to use (default: $font)"
2024-08-08 14:42:14 -06:00
echo " -i, --input the input file (required)"
echo " -o, --output the output file (default: input)"
echo " --help display this help and exit"
}
while test $# -gt 0; do
case $1 in
-f|--font)
font="$2"
shift; shift
;;
-i|--input)
input="$2"
shift; shift
;;
-o|--output)
output="$2"
shift; shift
;;
--help)
usage
exit 0
;;
-*|--*)
usage
exit 2
;;
*)
if test -z "$caption";
then caption="$1"
else caption="$caption $1"
fi
shift
;;
esac
done
if test -z "$input"; then
usage
exit 2
fi
if test -z "$output"; then
output="captioned-`basename "$input"`"
fi
2024-08-08 15:47:08 -06:00
2024-08-08 14:42:14 -06:00
if ! test -f "$input"; then
echo "$name: $input not found"
exit 1
fi
captionWidth="512x"
captionTemp=`mktemp --suffix=".png"`
2024-08-08 15:35:44 -06:00
imageWidth=`identify -ping -format "%[w]x\n" "$input" | head -n 1`
2024-08-08 14:42:14 -06:00
echo "$name: generating caption..."
convert \
-background white -fill "#010000" -font "$font" \
-pointsize 55 -size "$captionWidth" -gravity Center \
caption:"$caption" \
-bordercolor white -border 20x20 \
2024-08-08 15:47:08 -06:00
"$captionTemp" || exit 1
2024-08-08 14:42:14 -06:00
echo "$name: compositing over $input..."
# https://www.imagemagick.org/discourse-server/viewtopic.php?t=33797
convert \
"$captionTemp" -resize "$imageWidth" \
-background white \( "$input" -coalesce \) \
-set page "%[fx:u.w]x%[fx:u.h+v.h]+%[fx:t?(u.w-v.w)/2:0]+%[fx:t?u.h:0]" -coalesce \
null: -insert 1 -layers composite -layers optimize \
2024-08-08 15:47:08 -06:00
"$output" || exit 1
2024-08-08 14:42:14 -06:00
rm "$captionTemp"
echo "$name: done!"