my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

3 years ago
  1. ################################################################################
  2. # catimg script by Eduardo San Martin Morote aka Posva #
  3. # https://posva.net #
  4. # #
  5. # Output the content of an image to the stdout using the 256 colors of the #
  6. # terminal. #
  7. # GitHub: https://github.com/posva/catimg #
  8. ################################################################################
  9. function help() {
  10. echo "Usage catimg [-h] [-w width] [-c char] img"
  11. echo "By default char is \" \" and w is the terminal width"
  12. }
  13. # VARIABLES
  14. COLOR_FILE=$(dirname $0)/colors.png
  15. CHAR=" "
  16. WIDTH=""
  17. IMG=""
  18. while getopts qw:c:h opt; do
  19. case "$opt" in
  20. w) WIDTH="$OPTARG" ;;
  21. c) CHAR="$OPTARG" ;;
  22. h) help; exit ;;
  23. *) help ; exit 1;;
  24. esac
  25. done
  26. while [ "$1" ]; do
  27. IMG="$1"
  28. shift
  29. done
  30. if [ "$IMG" = "" -o ! -f "$IMG" ]; then
  31. help
  32. exit 1
  33. fi
  34. if [ ! "$WIDTH" ]; then
  35. COLS=$(expr $(tput cols) "/" $(echo -n "$CHAR" | wc -c))
  36. else
  37. COLS=$(expr $WIDTH "/" $(echo -n "$CHAR" | wc -c))
  38. fi
  39. WIDTH=$(convert "$IMG" -print "%w\n" /dev/null)
  40. if [ "$WIDTH" -gt "$COLS" ]; then
  41. WIDTH=$COLS
  42. fi
  43. REMAP=""
  44. if convert "$IMG" -resize $COLS\> +dither -remap $COLOR_FILE /dev/null ; then
  45. REMAP="-remap $COLOR_FILE"
  46. else
  47. echo "The version of convert is too old, don't expect good results :(" >&2
  48. #convert "$IMG" -colors 256 PNG8:tmp.png
  49. #IMG="tmp.png"
  50. fi
  51. # Display the image
  52. I=0
  53. convert "$IMG" -resize $COLS\> +dither `echo $REMAP` txt:- 2>/dev/null |
  54. sed -e 's/.*none.*/NO NO NO/g' -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
  55. while read R G B f; do
  56. if [ ! "$R" = "NO" ]; then
  57. if [ "$R" -eq "$G" -a "$G" -eq "$B" ]; then
  58. ((
  59. I++,
  60. IDX = 232 + R * 23 / 255
  61. ))
  62. else
  63. ((
  64. I++,
  65. IDX = 16
  66. + R * 5 / 255 * 36
  67. + G * 5 / 255 * 6
  68. + B * 5 / 255
  69. ))
  70. fi
  71. #echo "$R,$G,$B: $IDX"
  72. echo -ne "\e[48;5;${IDX}m${CHAR}"
  73. else
  74. (( I++ ))
  75. echo -ne "\e[0m${CHAR}"
  76. fi
  77. # New lines
  78. (( $I % $WIDTH )) || echo -e "\e[0m"
  79. done