Skip to content

Instantly share code, notes, and snippets.

@kana
Created May 23, 2019 13:50
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 kana/90b54c38ad149c80d1f5bafbe1508a66 to your computer and use it in GitHub Desktop.
Save kana/90b54c38ad149c80d1f5bafbe1508a66 to your computer and use it in GitHub Desktop.
NES-ish image color reduction
#!/bin/bash
if ! [ "$#" = 1 ]
then
echo "Usage: $0 FILE"
exit 1
fi
d="$(identify "$1" | cut -d' ' -f3)"
width="${d/x*/}"
height="${d/*x/}"
width_d="$(echo -n "$width" | wc -c)"
height_d="$(echo -n "$height" | wc -c)"
basename="${1/.*/}"
# https://commons.wikimedia.org/wiki/File:NES_palette.png#/media/File:NES_palette.png
convert "$1" -dither FloydSteinberg -remap nes-palettes.png -colors 25 ",$basename-palettes.png"
for x in $(seq 0 16 $((width - 1)))
do
for y in $(seq 0 16 $((height - 1)))
do
# convert "$1" -crop "16x16+$x+$y" +dither -colors 4 -remap ",$basename-palettes.png" "$(printf ",$basename-sprite-%0${width_d}d-%0${height_d}d.png" $x $y)"
convert "$1" -crop "16x16+$x+$y" -dither FloydSteinberg -colors 4 -remap ",$basename-palettes.png" "$(printf ",$basename-sprite-%0${width_d}d-%0${height_d}d.png" $x $y)"
done
done
for x in $(seq 0 16 $((width - 1)))
do
x="$(printf "%0${width_d}d" "$x")"
convert ,$basename-sprite-$x-*.png -append ,$basename-sprite-v-$x.png
done
convert ,$basename-sprite-v-*.png -flatten $basename-nes.png
rm ,$basename-*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment