Last active
July 19, 2016 04:33
-
-
Save JarLowrey/fbf7c76ffe46dbeba0a61833c305c096 to your computer and use it in GitHub Desktop.
Searches in subdirectories for png files with names "frame-1.png", "frame-2.png"... and so on. Then makes a horizontal or vertical spritesheet out of the individual frames.
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
#!/bin/bash | |
#Directory setup: Individual images constituting one spritesheet must be by themselves in their own directory. | |
# All images must be '.png' files with that extension in their name. | |
# In a spritesheet directory, the images must be name frame-1.png thru frame-LAST_FRAME_NUMBER.png | |
#utilizes ImageMagick for spritesheet creation | |
#copied from http://askubuntu.com/questions/179898/how-to-round-decimals-using-bc-in-bash | |
round() | |
{ | |
echo $(printf %.f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc)) | |
}; | |
imgWidth(){ | |
WIDTH=$(convert $1 -print %w /dev/null) | |
HEIGHT=$(convert $1 -print %h /dev/null) | |
#do floating point division with the 'bc' command | |
#I expect the images to be about 120 pixels. Thus use that to approximate. This may be wrong, and the output will need to be checked in Phaser | |
NUM_FRAMES=$(bc -l <<< "$WIDTH / 120.0") | |
NUM_FRAMES=$(round $NUM_FRAMES 10) #Round to nearest integer | |
INDIVIDUAL_WIDTH=$(bc -l <<< "$WIDTH / $NUM_FRAMES") | |
echo $INDIVIDUAL_WIDTH | |
} | |
imgHeight(){ | |
HEIGHT=$(convert $1 -print %h /dev/null) | |
echo $HEIGHT | |
} | |
#arg1 is the output file path (with file name) | |
#arg2 is the path of the file to add | |
#arg3 is the frame # we're on | |
#arg4 is the -h or -v flag | |
concatImg(){ | |
HEIGHT=$(imgHeight "$2") | |
WIDTH=$(imgWidth "$2") | |
if [ "$4" == "-h" ] ; then | |
montage "$2" -tile "$3"x1 -geometry "$WIDTH"x"$HEIGHT"+0+0 -background transparent "$1" | |
elif [[ "$4" == "-v" ]] ; then | |
montage "$2" -tile 1x"$3" -geometry "$WIDTH"x"$HEIGHT"+0+0 -background transparent "$1" | |
else | |
echo "compact spritesheets not supported. Add it in yourself, or add a '-h' or '-v' argument" | |
fi | |
} | |
#argument 1 is the parent directory of images | |
#arg 2 is the output directory | |
#argument 3 is '-v' (vertical) or '-h' (horizontal), the type of spritesheet that will be made | |
concatenateImgs() { | |
echo "Autocropping each png to remove empty space around edges before making them into a spritesheet" | |
for entry in "$IMG_DIR"/*.png | |
do | |
echo " Cropped $entry" | |
convert $entry -trim $entry | |
done | |
#echo "making spritesheet out of $NUM_PNGS_IN_THIS_DIR images" | |
echo "making spritesheet out of all images" | |
FRAME_NUM=1 | |
for entry in "$IMG_DIR"/*.png | |
do | |
if [ "$FRAME_NUM" = 1 ] ; then | |
cp "$entry" "$2" | |
else | |
echo "2 SEEN!!!" | |
concatImg "$2" "$entry" $FRAME_NUM $3 | |
fi | |
FRAME_NUM="$(($FRAME_NUM+1))" | |
done | |
#double check by trimming after making spritesheet too | |
convert $2 -trim $2 | |
} | |
echo $0 | |
read -e -p "Directory with images:" IMG_DIR | |
IMG_DIR="${IMG_DIR/#~/$HOME}" #substitute a '~' at the beginning of string with $HOME. http://stackoverflow.com/questions/38364603/using-a-variable-directory-name-in-bash-prevents-me-from-seeing-files-in-that-di/38364929#38364929 | |
#read -e -p "Output directory:" OUTPUT_DIR | |
OUTPUT_PATH="$HOME/Desktop/spritesheet.png" | |
concatenateImgs "$IMG_DIR" "$OUTPUT_PATH" -h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment