1
0
Fork 0
mirror of https://github.com/sbrl/bin.git synced 2018-01-10 21:33:46 +00:00

Added catimg

This commit is contained in:
Starbeamrainbowlabs 2016-08-12 21:45:08 +02:00
parent 0d43bc7d2b
commit 31c3ca1c8c
2 changed files with 132 additions and 0 deletions

View file

@ -34,6 +34,9 @@ Here's a list of the most interesting ones, along with what they do and where th
- [quickshare](https://github.com/netham91/Quick-Share) - A quick way to share files via HTTP.
- [cscz](https://git.starbeamrainbowlabs.com/sbrl/cscz) - The one and only C# class generator. A little timesaving tool I built myself, written in C# or course :D
- [repren](https://github.com/jlevy/repren) - A good bulk file renaming tool.
- [goldilocks](http://unix.stackexchange.com/a/124460/64687) - A script that displays all of the possible terminal colours.
- [catimg](https://github.com/posva/catimg) - A cool script to display images in the terminal. Useful when you're sshing into
servers.
## Disclaimer
I don't own many of the tools in this repository. If you are an owner of one of these tools and I haven't linked to you, please let me know as I probably forgot.

129
catimg Executable file
View file

@ -0,0 +1,129 @@
#!/usr/bin/env bash
################################################################################
# catimg script by Eduardo San Martin Morote aka Posva #
# http://posva.net #
# #
# Ouput the content of an image to the stdout using the 256 colors of the #
# terminal. #
# Github: https://github.com/posva/catimg #
################################################################################
help() {
echo "Usage catimg [-h] [-w width] [-c char] img"
echo "By default char is \" \" and w is the terminal width"
}
# VARIABLES
COLOR_FILE=$(dirname "$0")/catimg-ext-colors.png
CHAR=" "
WIDTH=""
IMG=""
LOOP=""
while getopts w:c:l:h opt; do
case "$opt" in
w) WIDTH="$OPTARG" ;;
c) CHAR="$OPTARG" ;;
h) help; exit ;;
l) LOOP="$OPTARG" ;;
*) help ; exit 1;;
esac
done
while [ "$1" ]; do
IMG="$1"
shift
done
if [ "$IMG" = "" -o ! -f "$IMG" ]; then
help
exit 1
fi
if [ ! "$WIDTH" ]; then
(( COLS = $(tput cols) / $(echo -n "$CHAR" | wc -c) ))
else
(( COLS = WIDTH / $(echo -n "$CHAR" | wc -c) ))
fi
WIDTH=$(convert "$IMG" -print "%w\n" /dev/null)
if [ "$WIDTH" -gt "$COLS" ]; then
WIDTH=$COLS
fi
if ! convert "$IMG" -resize "${COLS}>" +dither -remap "${COLOR_FILE}" /dev/null ; then
echo "The version of convert is too old, don't expect good results :(" >&2
fi
# produce terminal color from rgb value
# term_color r g b
term_color() {
local r g b
r="$1"
g="$2"
b="$3"
if [ "$r" -eq "$g" -a "$g" -eq "$b" ]; then
((
IDX = 232 + r * 23 / 255
))
else
# the * 36 at the end is intended, it gives better results
((
IDX = 16
+ r * 5 / 255 * 36
+ g * 5 / 255 * 6
+ b * 5 / 255
))
fi
}
# Display the image
L=0
if [ ! "$LOOP" ]; then
LOOP=1
fi
IMAGE="" # Stored image, used when looping
I=0
while [[ "$L" -lt "${LOOP}" || "${LOOP}" = 0 ]]; do
IND=1 # for array storing
convert "$IMG" -resize $COLS\> +dither -remap "${COLOR_FILE}" txt:- |
sed -e 's/.*none.*/NO NO NO/g' -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
while read R G B f; do
(( I++ ))
if [ "$R" = "#" ]; then
echo -en "\033[${I}A" # Move up the cursor
I=0
elif [ ! "$R" = "NO" ]; then
if [ "$LOOP" ]; then
if [ ! "${PIXEL[IND]}" ]; then
term_color "$R" "$G" "$B"
#echo "$R,$G,$B: $IDX"
PIXEL[IND]="[48;5;${IDX}m${CHAR}"
fi
echo -n "${PIXEL[IND]}"
IMAGE="${IMAGE}${PIXEL[IND]}"
(( IND++ ))
#echo "IND = ${IND}"
else
term_color "$R" "$G" "$B"
#echo "$R,$G,$B: $IDX"
echo -n "[48;5;${IDX}m${CHAR}"
IMAGE="${IMAGE}[48;5;${IDX}m${CHAR}"
fi
else
echo -n "${CHAR}"
IMAGE="${IMAGE}${CHAR}"
fi
# New lines
(( K = I % WIDTH ))
if [[ "$K" = 0 ]]; then
echo ""
IMAGE="${IMAGE}
"
fi
done
echo -en "\033[${I}A" # Move up the cursor
((L++))
done