Skip to content

Instantly share code, notes, and snippets.

@wallentx
Created October 6, 2015 19:05
Show Gist options
  • Save wallentx/d0bb89a6d42f5b5afbd4 to your computer and use it in GitHub Desktop.
Save wallentx/d0bb89a6d42f5b5afbd4 to your computer and use it in GitHub Desktop.
convert small images like sprites to terminal ready ascii art. Not mine. I forgot where i downloaded it from.
#!/bin/bash
if [ $# -eq 0 ]; then
echo -e " \n \t ERROR: No arguments provided"
echo -e " \t USAGE: img2sf <path/to/image> <path/to/output>\n"
exit 1
fi
imgw=$(convert -print "%w\n" "$1" /dev/null)
# generate the image
TMPFILE="tmp"
img2xterm "$1" > $TMPFILE
# form the output file
#echo 'startline="0"' > $2
#echo 'fulloutput=(' >> $2
IFS=''
cat $TMPFILE |
while read line
do
nspaces=$(grep -o " " <<< "$line" | wc -l)
nup=$(grep -o "▀" <<< "$line" | wc -l)
ndown=$(grep -o "▄" <<< "$line" | wc -l)
spaces=$(expr "$imgw" - "$nspaces" - "$nup" - "$ndown")
# printf "\"%s%${spaces}s %%s\"\n" "$line" "" >> $2
printf "%s%${spaces}s \\n" "$line" >> $2
done
#echo ")" >> $2
sed -i "0,/%s/{s/%s/ %s/}" "$2"
# clean up
rm $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment