Skip to content

Instantly share code, notes, and snippets.

@AkBKukU
Created July 17, 2018 19:13
Show Gist options
  • Save AkBKukU/f6cec1e6e8a1197b984ac7e200797f9e to your computer and use it in GitHub Desktop.
Save AkBKukU/f6cec1e6e8a1197b984ac7e200797f9e to your computer and use it in GitHub Desktop.
Stitch, trim, and make a grid of images to make a sprite sheet
#!/bin/bash
echo "Stitching Sprites"
function getImageFiles ()
{
# Get images name
dirname="$(pwd | tr '/' ' ' | awk '{print $(NF)}')"
filename="$dirname"
files=($(ls))
fileCount="${#files[@]}"
# limit to pngs
for (( i = 0; i < $fileCount; i++ )); do
testName="${files[$i]}"
extension="${testName##*.}"
if [ "$extension" == "png" ]
then
pngs=" $pngs $testName "
fi
done
files=($(echo "$pngs"))
fileCount="${#files[@]}"
}
function calcSheetSize ()
{
# Get tiles needed
for (( i = 0; i < $fileCount; i++ )); do
testSize="$( expr $i \* $i )"
if [ $testSize -ge $fileCount ]
then
width="$i"
break
fi
done
echo "Sheet size: $width x $width"
}
function getBaseImageInfo ()
{
# Layer all rendered imaged together
convert "${files[0]}" "${files[1]}" -composite maxres.png # start with first two
for (( i = 2; i < $fileCount; i++ )); do
convert maxres.png "${files[$i]}" -composite maxres.png
done
# Get the offset for the crop of the layered images
offsetxsize="$(convert maxres.png -trim info: | awk '{print $4}' | tr '+' ' ' | awk '{print $2}')"
offsetysize="$(convert maxres.png -trim info: | awk '{print $4}' | tr '+' ' ' | awk '{print $3}')"
# Get the dimensions of the layer images.
trimxsize="$(convert maxres.png -trim info: | awk '{print $3}' | tr 'x' ' ' | awk '{print $1}')"
trimysize="$(convert maxres.png -trim info: | awk '{print $3}' | tr 'x' ' ' | awk '{print $2}')"
# Delete uneedded maxres
rm maxres.png
}
function baseTrim ()
{
# Crop all images to the dimiseniton and offset fo the trimed layered image
for (( i = 0; i < $fileCount; i++ )); do
convert "${files[$i]}" -crop $(echo $trimxsize)x$trimysize+$offsetxsize+$offsetysize "${files[$i]%.*}_temp.png"
done
}
function buildSheet ()
{
# Put images in rows
for (( row = 0; row < $width; row++ )); do
convert -size 0x0 xc:blue "line-$row.png"
for (( col = 0; col < $width; col++ )); do
if [ $(expr $(expr $row \* $width) + $col) -ge $fileCount ]
then
break
fi
convert "line-$row.png" -background none "${files[$(expr $(expr $row \* $width) + $col)]%.*}_temp.png" +append "line-$row.png"
rm "${files[$(expr $(expr $row \* $width) + $col)]%.*}_temp.png"
done
done
# Put rows on top of eachother into output file and delete row images
if [ -f ../$filename.png ]
then
rm ../$filename.png
convert -size 0x0 xc:blue ../$filename.png
fi
convert ../$filename.png -background none "line-0.png" -append ../$filename.png
rm "line-0.png"
for (( row = 1; row < $width; row++ )); do
convert ../$filename.png -background none "line-$row.png" -append ../$filename.png
rm "line-$row.png"
done
}
function specialCaseTest ()
{
if [ $fileCount -eq 1 ]
then
echo "Single image found"
convert "${files[0]}" -trim ../$filename.png
exit 0
fi
if [ $fileCount -eq 2 ]
then
echo "Two images found"
getBaseImageInfo
convert "${files[0]}" -crop $(echo $trimxsize)x$trimysize+$offsetxsize+$offsetysize "${files[0]%.*}_temp.png"
convert "${files[1]}" -crop $(echo $trimxsize)x$trimysize+$offsetxsize+$offsetysize "${files[1]%.*}_temp.png"
convert "${files[0]%.*}_temp.png" -background none "${files[1]%.*}_temp.png" +append ../$filename.png
rm "${files[0]%.*}_temp.png" "${files[1]%.*}_temp.png"
exit 0
fi
}
# Logic
getImageFiles
specialCaseTest
calcSheetSize
getBaseImageInfo
baseTrim
buildSheet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment