# This script uses imagemagick to create grids of images and automatically adds captions to them.
# There are various grid styles which can be made outlined below
# Style 1 - n by m grid with caption for every row
# for a 3 by 2 grid it will look like:
# ------ ------
# | | | |
# | | | |
# ------ ------
# ------ ------
# | | | |
# | | | |
# ------ ------
# ------ ------
# | | | |
# | | | |
# ------ ------
#
####
#Help
####
Help(){
echo "fig_grid combines images into a grid, useful for creating complex figures that can be moved between pandoc/latex and other sources such as word or powerpoint."
#First calculate the number of inputs to be expected, then check to make sure enough images are provided
style=${args[0]}
calc_style="${style/x/\*}"
expfiles=$(echo $calc_style | bc)
if [ $expfiles -ne $(( argnum -1 )) ]; then
fail "expected $expfiles files but only got $(( argnum - 1 )) files"
fi
#Now read the caption file into an array if it exists
if [ "$captionfile" != "" ]; then
mapfile -t captions < $captionfile
fi
#Finally generate the correct images. By default this will size all images to the size of the largest image to create regular spacings. It does this by scaling and the extent command
convert -extent "$max_x"x"$max_y" -gravity center -border "$border"x$border -bordercolor black $tmp$i.png $tmp$i.png || fail "Failed to add padding to image"