0185d9920b7d6c380277a4a15c9e4818df41700a

Author
Eduardo San Martin Morote <posva13@gmail.com>
Committer
Eduardo San Martin Morote <posva13@gmail.com>
Date

Message

catimg plugin
allow to print an image to the stdout using convert

Diff

  1diff --git a/plugins/catimg/catimg.plugin.zsh b/plugins/catimg/catimg.plugin.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..01e81eca70671d3b89fbef546b22b181fc5d61ba
  4--- /dev/null
  5+++ b/plugins/catimg/catimg.plugin.zsh
  6@@ -0,0 +1,16 @@
  7+################################################################################
  8+# catimg script by Eduardo San Martin Morote aka Posva                         #
  9+# http://posva.net                                                             #
 10+#                                                                              #
 11+# Ouput the content of an image to the stdout using the 256 colors of the      #
 12+# terminal.                                                                    #
 13+# Github: https://github.com/posva/catimg                                      #
 14+################################################################################
 15+
 16+if [[ -x  `which convert` ]]; then
 17+  function catimg() {
 18+    source $(dirname $0)/catimg.sh $@
 19+  }
 20+else
 21+  echo "catimg need convert (ImageMagick) to work)"
 22+fi
 23diff --git a/plugins/catimg/catimg.sh b/plugins/catimg/catimg.sh
 24new file mode 100755
 25index 0000000000000000000000000000000000000000..f9e067e348993dcffa68a2e356cd84243301ef72
 26--- /dev/null
 27+++ b/plugins/catimg/catimg.sh
 28@@ -0,0 +1,87 @@
 29+################################################################################
 30+# catimg script by Eduardo San Martin Morote aka Posva                         #
 31+# http://posva.net                                                             #
 32+#                                                                              #
 33+# Ouput the content of an image to the stdout using the 256 colors of the      #
 34+# terminal.                                                                    #
 35+# Github: https://github.com/posva/catimg                                      #
 36+################################################################################
 37+
 38+function help() {
 39+  echo "Usage catimg [-h] [-w width] [-c char] img"
 40+  echo "By default char is \"  \" and w is the terminal width"
 41+}
 42+
 43+# VARIABLES
 44+COLOR_FILE=$(dirname $0)/colors.png
 45+CHAR="  "
 46+
 47+WIDTH=""
 48+IMG=""
 49+
 50+while getopts qw:c:h opt; do
 51+  case "$opt" in
 52+    w) WIDTH="$OPTARG" ;;
 53+    c) CHAR="$OPTARG" ;;
 54+    h) help; exit ;;
 55+    *) help ; exit 1;;
 56+    esac
 57+  done
 58+
 59+while [ "$1" ]; do
 60+  IMG="$1"
 61+  shift
 62+done
 63+
 64+if [ "$IMG" = "" -o ! -f "$IMG" ]; then
 65+  help
 66+  exit 1
 67+fi
 68+
 69+if [ ! "$WIDTH" ]; then
 70+  COLS=$(expr $(tput cols) "/" $(echo -n "$CHAR" | wc -c))
 71+else
 72+  COLS=$(expr $WIDTH "/" $(echo -n "$CHAR" | wc -c))
 73+fi
 74+WIDTH=$(convert ${IMG} -print "%w\n" /dev/null)
 75+if [ "$WIDTH" -gt "$COLS" ]; then
 76+  WIDTH=$COLS
 77+fi
 78+
 79+REMAP=""
 80+if convert "$IMG" -resize $COLS\> +dither -remap $COLOR_FILE /dev/null ; then
 81+  REMAP="-remap $COLOR_FILE"
 82+else
 83+  echo "The version of convert is too old, don't expect good results :(" >&2
 84+  #convert "$IMG" -colors 256 PNG8:tmp.png
 85+  #IMG="tmp.png"
 86+fi
 87+
 88+# Display the image
 89+convert "$IMG" -resize $COLS\> +dither `echo $REMAP` txt:- |
 90+sed -e 's/.*none.*/NO NO NO/g' -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
 91+while read R G B f; do
 92+  if [ ! "$R" = "NO" ]; then
 93+    if [ "$R" -eq "$G" -a "$G" -eq "$B" ]; then
 94+      ((
 95+      I++,
 96+      IDX = 232 + R * 23 / 255
 97+      ))
 98+    else
 99+      ((
100+      I++,
101+      IDX = 16
102+      + R * 5 / 255 * 36
103+      + G * 5 / 255 * 6
104+      + B * 5 / 255
105+      ))
106+    fi
107+    #echo "$R,$G,$B: $IDX"
108+    echo -ne "\e[48;5;${IDX}m${CHAR}"
109+  else
110+    (( I++ ))
111+    echo -ne "\e[0m${CHAR}"
112+  fi
113+  # New lines
114+  (( $I % $WIDTH )) || echo -e "\e[0m"
115+done
116diff --git a/plugins/catimg/colors.png b/plugins/catimg/colors.png
117new file mode 100644
118index 0000000000000000000000000000000000000000..5f2c8126bc0d8911a9de5d3ffbd4e37372b41671
119Binary files /dev/null and b/plugins/catimg/colors.png differ