Skip to content

Instantly share code, notes, and snippets.

@LunarLambda
Created October 12, 2023 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LunarLambda/92477f0faf67009af29f4e0baa999e45 to your computer and use it in GitHub Desktop.
Save LunarLambda/92477f0faf67009af29f4e0baa999e45 to your computer and use it in GitHub Desktop.
Script for pixel-perfect scaling of Pokémon sprites or other pixel art for use as Discord or Mastodon emotes
#!/bin/bash
if [ $# -lt 2 ]; then
echo "usage: $0 <infile> <outfile>"
echo "use TARGET environment variable to set target size (default: 128)"
exit 1
fi
convert -trim +repage "$1" "$2"
read -r w h <<<"$(identify -format '%[fx:w] %[fx:h]' "$2")"
size=$(python3 <<EOF
d=max($w, $h)
print("{0}x{0}".format(d*(${TARGET:-128}//d)))
EOF
)
mogrify -sample "$size" "$2"
optipng -quiet "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment