Skip to content

Instantly share code, notes, and snippets.

@phx
Last active September 23, 2015 17:26
Show Gist options
  • Save phx/dea61821d2e55053ffd1 to your computer and use it in GitHub Desktop.
Save phx/dea61821d2e55053ffd1 to your computer and use it in GitHub Desktop.
OS X port of img2sf: A script to generate screenfetch ASCII from an image. Original img2sf project link: https://github.com/cbarox/scripts/tree/master/img2sf
Dependencies: img2xterm (not available via Homebrew -- you will need to build from source, see below)
Rossy's img2xterm project link: https://github.com/rossy/img2xterm
Notes: I had to re-install ImageMagick for img2xterm to work properly.
Instructions:
$ brew update
$ brew reinstall imagemagick
$ git clone https://github.com/rossy/img2xterm.git
$ cd img2xterm && make && make install
Original img2sf raw script: https://raw.githubusercontent.com/cbarox/scripts/master/img2sf/img2sf
Below is how I modified the img2sf script to work with OS X (tested on version 10.10.4):
Also:
Since OS X uses BSD sed, which doesn't support the -i flag, I originally modified the script to use GNU sed, which I installed via Homebrew. However, I have since modified the script to support the BSD version of sed that ships natively with OS X (or Xcode -- not sure). I would still advise using gsed, since the use of the -i flag is cleaner and doesn't require as much additional code and/or cleanup.
Changes:
Removed Line 16: echo 'startline="0"' > $2
Removed Line 17: echo 'fulloutput=(' >> $2
Modified Line 25: spaces=$(expr "$imgw" - "$nspaces" - "$nup" - "$ndown" 2>/dev/null)
Removed Line 28: echo ")" >> $2
Removed Line 30: sed -i "0,/%s/{s/%s/ %s/}" "$2"
Added Line 31: sed 's/"//g' "$2" > osx.tmp; mv osx.tmp "$2"
Added Line 32: sed 's/%s//g' "$2" > osx.tmp; mv osx.tmp "$2"
#!/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
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" 2>/dev/null)
printf "\"%s%${spaces}s %%s\"\n" "$line" "" >> $2
done
sed 's/"//g' "$2" > osx.tmp; mv osx.tmp "$2"
sed 's/%s//g' "$2" > osx.tmp; mv osx.tmp "$2"
# clean up
rm $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment