Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active July 14, 2021 21:35
Show Gist options
  • Save gingerbeardman/b85367a8e6b3d139d9c85f49e146af38 to your computer and use it in GitHub Desktop.
Save gingerbeardman/b85367a8e6b3d139d9c85f49e146af38 to your computer and use it in GitHub Desktop.
Convert an image table to an Animated GIF
#!/usr/bin/env bash
display_usage() {
echo "This script must be passed an image table PNG."
echo
echo "Usage: $0 image-table-32-32.png"
}
# if less than two arguments supplied, display usage
if [ $# -lt 1 ]
then
display_usage
exit 1
fi
# check whether user has supplied -h or --help, display usage
if [[ ( $# == "--help") || $# == "-h" ]]
then
display_usage
exit 0
fi
# -----
# input file details
fullname=$(basename -- "$1")
filename="${fullname%.*}"
# extract cell size from filename
regex_table=".*\-table\-([0-9]+)\-([0-9]+)\.png"
if [[ ${fullname} =~ ${regex_table} ]]
then
W="${BASH_REMATCH[1]}"
H="${BASH_REMATCH[2]}"
else
display_usage
exit 1
fi
# convert to Animated GIF
magick -dispose background -delay 1 -loop 0 "$1" +repage -crop "${W}x${H}" +repage "${filename}.gif"
# output specifications
magick identify -format "${filename}.gif: %n frames\n" "${filename}.gif" | head -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment