Skip to content

Instantly share code, notes, and snippets.

@iskolbin
Last active March 10, 2022 10:05
Show Gist options
  • Save iskolbin/2492653a48fc7a8f8c1f8a5eaf159eb7 to your computer and use it in GitHub Desktop.
Save iskolbin/2492653a48fc7a8f8c1f8a5eaf159eb7 to your computer and use it in GitHub Desktop.
Unpack TexturePacker JSON into images
#!/bin/bash
if [ -z $1 ]; then
echo "Usage:\n sh crop_atlas.sh <ATLAS>.json"
exit 1
fi
IMG=$(cat $1 | jq -j ".meta.image")
KEYS=$(cat $1 | jq -j --unbuffered '.frames|keys|join(" ")')
EXT="${IMG##*.}"
NAME="${IMG%.*}"
mkdir -p ${NAME}
mkdir -p ${NAME}
for F in $(cat $1 | jq -r ".frames|to_entries|.[]|.key" | rev | cut -d'/' -f2- | rev); do
mkdir -p ${NAME}/$F
done
for s in $(cat $1 | jq -j --unbuffered ".frames|to_entries|.[] | select(.value.rotated == false) | \"${NAME}/\",.key,\".${EXT}\n\""); do mkdir -p "$(dirname $s)"; done
cat $1 | jq -j --unbuffered ".frames|to_entries|.[] | select(.value.rotated == false) | \"convert $IMG -background none -crop \",.value.frame.w,\"x\",.value.frame.h,\"+\",.value.frame.x,\"+\",.value.frame.y, \" -extent \",.value.sourceSize.w,\"x\",.value.sourceSize.h,\"-\",.value.spriteSourceSize.x,\"-\",.value.spriteSourceSize.y, \" ${NAME}/\",.key,\".${EXT} \n\"" | bash
cat $1 | jq -j --unbuffered ".frames|to_entries|.[] | select(.value.rotated == true) | \"convert $IMG -background none -crop \",.value.frame.h,\"x\",.value.frame.w,\"+\",.value.frame.x,\"+\",.value.frame.y, \" -rotate -90 -extent \",.value.sourceSize.w,\"x\",.value.sourceSize.h,\"-\",.value.spriteSourceSize.x,\"-\",.value.spriteSourceSize.y, \" ${NAME}/\",.key,\".${EXT} \n\"" | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment