Skip to content

Instantly share code, notes, and snippets.

@geta6
Last active January 23, 2016 22:35
Show Gist options
  • Save geta6/5796089 to your computer and use it in GitHub Desktop.
Save geta6/5796089 to your computer and use it in GitHub Desktop.
cat image on xterm256 terminal, zsh or bash
#!/bin/bash
# Modified by Yu-Jie Lin for Bash from zsh version
ORG="$1"
if [[ ${ORG##*.} =~ JPG|jpg|JPEG|jpeg|GIF|gif|PNG|png ]]; then
CATWIDTH=$(tput cols)
SRC=$(mktemp).png
convert -resize $(( CATWIDTH / 2 ))x "$ORG" "$SRC"
W=$(identify "$SRC" | sed -e 's/^.* \([0-9]*\)x[0-9]* .*$/\1/g')
I=0
convert "$SRC" -crop 1x1+$W txt:- 2>/dev/null |
sed -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
while read R G B _; do
((
I++,
IDX = 16
+ R * 5 / 255 * 36
+ G * 5 / 255 * 6
+ B * 5 / 255
))
echo -ne "\x1b[48;5;${IDX}m \x1b[0m"
(( $I % $W )) || echo
done
rm "$SRC"
else
cat $@
fi
#!/bin/zsh
# Orginal by geta6
ORG=$1
if [[ ${ORG##*.} =~ '(JPG|jpg|JPEG|jpeg|GIF|gif|PNG|png)' ]]; then
CATWIDTH=128
SRC=/tmp/termtmp.png
convert -resize $(( $CATWIDTH / 2 ))x "$ORG" "$SRC"
INF=`identify "$SRC" | sed -e 's/^.* \([0-9]*x[0-9]*\) .*$/\1/g'`
W=`echo "$INF" | cut -d'x' -f1`
INF=`convert "$SRC" -crop 1x1+$W txt:- 2>&/dev/null`
INF=`echo $INF | sed -e 's/^.*(\(.*\)[,)].*$/\1/g' | sed -e 's/,/ /g' | sed -e '1,1d'`
I=0
RGB2COL()
{
I=$(( $I + 1 ))
_R=$(( $1 * 5 / 255 * 36 ))
_G=$(( $2 * 5 / 255 * 6 ))
_B=$(( $3 * 5 / 255 ))
echo -n "\x1b[48;5;$(( 16 + $_R + $_G + $_B ))m \x1b[0m"
[[ 0 -eq $(( $I % $W )) ]] && echo -n '\n'
}
I=0
echo $INF | while read line
do
RGB2COL `echo $line`
done
else
cat $@
fi
@livibetter
Copy link

I ported zsh to Bash in my Gist fork, please pull it for Bash users. Would you also license your script? If not, I will remove my fork.


2013-12-22T21:57:29Z: since my modification has been included, I deleted my fork, though it should've been pulled in from my fork instead of copy-and-pasting.

@geta6
Copy link
Author

geta6 commented Aug 23, 2013

thx!

@posva
Copy link

posva commented Dec 22, 2013

Thanks to your version I was able to update mine

I wanted to tell you that you're forgetting transparent colors and grayscale and that's a shame 😄
Anyways, thank you very much for your helpful version of catimg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment