Last active
January 23, 2016 22:35
-
-
Save geta6/5796089 to your computer and use it in GitHub Desktop.
cat image on xterm256 terminal, zsh or bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
thx!