1 ################################################################################
2 # catimg script by Eduardo San Martin Morote aka Posva #
5 # Output the content of an image to the stdout using the 256 colors of the #
7 # GitHub: https://github.com/posva/catimg #
8 ################################################################################
11 echo "Usage catimg [-h] [-w width] [-c char] img"
12 echo "By default char is \" \" and w is the terminal width"
16 COLOR_FILE=$(dirname $0)/colors.png
22 while getopts qw:c:h opt; do
36 if [ "$IMG" = "" -o ! -f "$IMG" ]; then
41 if [ ! "$WIDTH" ]; then
42 COLS=$(expr $(tput cols) "/" $(echo -n "$CHAR" | wc -c))
44 COLS=$(expr $WIDTH "/" $(echo -n "$CHAR" | wc -c))
46 WIDTH=$(convert "$IMG" -print "%w\n" /dev/null)
47 if [ "$WIDTH" -gt "$COLS" ]; then
52 if convert "$IMG" -resize $COLS\> +dither -remap $COLOR_FILE /dev/null ; then
53 REMAP="-remap $COLOR_FILE"
55 echo "The version of convert is too old, don't expect good results :(" >&2
56 #convert "$IMG" -colors 256 PNG8:tmp.png
62 convert "$IMG" -resize $COLS\> +dither `echo $REMAP` txt:- 2>/dev/null |
63 sed -e 's/.*none.*/NO NO NO/g' -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
64 while read R G B f; do
65 if [ ! "$R" = "NO" ]; then
66 if [ "$R" -eq "$G" -a "$G" -eq "$B" ]; then
69 IDX = 232 + R * 23 / 255
80 #echo "$R,$G,$B: $IDX"
81 echo -ne "\e[48;5;${IDX}m${CHAR}"
84 echo -ne "\e[0m${CHAR}"
87 (( $I % $WIDTH )) || echo -e "\e[0m"