Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman

gingerbeardman/to-table.sh

Last active Jul 14, 2021
Embed
What would you like to do?
Convert an Animated GIF to an image table
#!/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