Last active
July 14, 2021 21:42
-
-
Save gingerbeardman/2a79913a883b2a675ce6a8004ebd8d4c to your computer and use it in GitHub Desktop.
Convert an Animated GIF to an image table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
display_usage() { | |
echo "This script must be passed an Animated GIF." | |
echo | |
echo "Usage: $0 image-table-32-32.gif [width-in-cells]" | |
} | |
# 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_anim=".*\-table\-([0-9]+)\-([0-9]+)\.gif" | |
if [[ ${fullname} =~ ${regex_anim} ]] | |
then | |
W="${BASH_REMATCH[1]}" | |
H="${BASH_REMATCH[2]}" | |
else | |
display_usage | |
exit 1 | |
fi | |
# check argument 2 is a number | |
regex_number='^[0-9]+$' | |
if ! [[ ${2} =~ ${regex_number} ]] | |
then | |
echo "error: [width-in-cells] needs to be a number" >&2 | |
exit 2 | |
fi | |
# if argument 2 is passed set tile parameter | |
if [[ $2 ]] | |
then | |
tile="-tile ${2}x" | |
else | |
tile="" | |
fi | |
# convert to image table PNG | |
magick montage "$1" $tile -geometry "1x1+0+0<" -alpha On -background transparent "${filename}.png" | |
# output specifications | |
magick identify -format "${filename}.png: %w x %h pixels\n" "${filename}.png" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment