Skip to content

Instantly share code, notes, and snippets.

@marjamis
Last active October 29, 2023 12:24
Show Gist options
  • Save marjamis/06eafed6b6a4c7048cf8744030f18e0b to your computer and use it in GitHub Desktop.
Save marjamis/06eafed6b6a4c7048cf8744030f18e0b to your computer and use it in GitHub Desktop.
Takes a folder of images and combines them together in rows and columns for a one output image, using the ImageMagick CLI
# Loops through all the .png images in the current folder and assigns their filename to an element of the array
counter=0
for i in `ls -1 ./*.png` ; do
images[$counter]=$i;
let counter=counter+1;
done
# Loops through the images array, based on the ROWS and COLS spec, to determine how many items rows and columns there will be when merging. Plenty of improvements here
ROWS=2
COLS=4
for (( c=0; c<$ROWS; c++ )) ; do
# Specifies the start of the images array to be used per row
imageIndexStart=$(expr $c \* $COLS)
# Specifies the end of the images array to be used per row
imageIndexEnd=$(expr $imageIndexStart + $COLS - 1)
echo "ROW = $c; RANGE = $imageIndexStart - $imageIndexEnd"
# Creates a string of the images to be composed into a row
l=""
for (( j=$imageIndexStart; j<=$imageIndexEnd; j++ )) ; do
echo ${images[j]}
l=$(echo $l ${images[j]})
done
# Using the string of images makes the row into it's own image
convert $l +append row_$c.png
done
# Using each image created for a row merge these all together for the final image
convert $(ls row_*.png) -append output.png
# Delete the intermediate row images
rm row_*.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment