You are not logged in.
Pages: 1
Hi.
Needed a simple terminal image viewer without many deps but found no suitable program. So I wrote my one shell script:
#!/bin/bash
# check parameters
while [[ $# -gt 0 ]]; do
case $1 in
-h)
echo "Usage: siv [OPTION]... FILE"
echo "shell image viewer"
echo
echo " -c <COLORS> Set the amount of colors."
echo " possible values: 8, 256"
echo " -h Display this help message."
echo " -s <SIZE> Set the destination size."
echo " possible values: <WIDTH>, <WIDTH>x<HEIGHT>"
echo " default is the terminal size"
echo " -v Display the version on siv."
exit 0;;
-v)
echo "siv 0.2"
echo "Copyright (C) 2010 Andreas Schönfelder"
exit 0;;
-s)
shift
size=$1;;
-c)
shift
if [[ $1 != 8 && $1 != 256 ]]; then
echo "'$1' colors not supported"
exit 1
fi
colors=$1;;
-*)
echo "siv: unrecognized option '$1'"
echo "Usage: siv [OPTIONS] FILE"
echo "Try 'siv -h' for more information."
exit 1;;
*)
if [[ ! -f "$1" ]]; then
echo "'$1' is not a file"
exit 1;
fi
file=$1;;
esac
shift
done
# set default values
[[ -z "$file" ]] && echo "no file set" && exit 1
[[ -z "$size" ]] && size=$(/bin/stty size | sed "s|\(\w*\) \(\w*\)|\2x\1|")
[[ -z "$colors" ]] && colors=256
case "$colors" in
256)
devideby=51;;
8)
devideby=128;;
esac
# convert image
i=1
x=1
i_line=1
old="$IFS"
IFS=$'\n'
for line in $(convert -compress none -depth 8 -filter box -resize "$size" "$file" ppm:-); do
IFS="$old"
# size
[[ $i_line == 2 ]] && width=$(($(echo $line | cut -d" " -f1)+1))
# jump header
if [[ $i_line < 4 ]]; then
i_line=$((i_line+1))
continue
fi
# show image
for value in $line; do
case "$i" in
1)
r=$((value/devideby));;
2)
g=$((value/devideby));;
3)
b=$((value/devideby))
case "$colors" in
256)
echo -en "\e[48;5;$((16+r*36+g*6+b))m \e[0m";;
8)
case "$r$g$b" in
000) # black
echo -en "\e[40m \e[0m";;
100) # red
echo -en "\e[41m \e[0m";;
010) # green
echo -en "\e[42m \e[0m";;
110) # yellow
echo -en "\e[43m \e[0m";;
001) # blue
echo -en "\e[44m \e[0m";;
101) # purple
echo -en "\e[45m \e[0m";;
011) # cyan
echo -en "\e[46m \e[0m";;
111) # white
echo -en "\e[47m \e[0m";;
esac;;
esac;;
esac
# next pixel
if [[ $i == 3 ]]; then
i=1
x=$((x+1))
else
i=$((i+1))
fi
# next line
[[ $x == $width ]] && echo && x=1
done
doneusage:
$ ./siv -h
Usage: siv [OPTION]... FILE
shell image viewer
-c <COLORS> Set the amount of colors.
possible values: 8, 256
-h Display this help message.
-s <SIZE> Set the destination size.
possible values: <WIDTH>, <WIDTH>x<HEIGHT>
default is the terminal size
-v Display the version on siv.It's really lightweight and without many deps. Only imagemagick is needed.
But it's not that fast. Perhaps somebody has a good small speed up tip.
That's it! Andreas
Last edited by echasslau (2010-10-05 15:26:52)
Offline
I got this
./siv.sh: line 95: syntax error near unexpected token `-en'
./siv.sh: line 95: ` echo -en "\e[44m \e[0m";;'I believe there is a missing line
001) # bluebefore line 95.
Offline
this is neat, produces much more "visible" images than img2txt.
a couple things at first glance:
use /bin/bash not sh if you're using bashisms ($( ), $(( )), etc).
you've got a typo or paste fail at your case 110 (yellow).
since you're bashy anyway, use [[ and ]], because they're faster than [ and ].
perhaps use the environment variables $COLS and $ROWS vs that tty | sed line you have now.
anyways, nice script!
/edit: it appears i meant $COLUMNS and $LINES, not $COLS and $ROWS... sorry.
Last edited by brisbin33 (2010-10-05 14:38:24)
//github/
Offline
ok, must be a copy&paste error
fixed it in the first post
Offline
perhaps use the environment variables $COLS and $ROWS vs that tty | sed line you have now.
I don't have $ROWS, but I do have $LINES.
Last edited by karol (2010-10-05 14:31:53)
Offline
brisbin33 wrote:perhaps use the environment variables $COLS and $ROWS vs that tty | sed line you have now.
I don't have $ROWS, but I do have $LINES.
You're right. I also don't have $COLS but $COLUMNS... wonder why i remember it wrong... anyway, edited my post.
//github/
Offline
Thanks for the advices. Updated...
No massive changes but now it doesn't need any tmp file.
Offline
since you're bashy anyway, use [[ and ]], because they're faster than [ and ].
I didn't know that, thanks for bringing it up.
Offline
That's very sexy. ![]()
However, at least on my machines, it's not very fast. I can watch every single line appear on the screen.
(That's a nice "retro-feeling", though.) Have you considered using something like awk? I bet that's a lot faster.
Another idea: I think most fonts don't use "squared" characters. For example, when using Terminus 16, a character is 8x16 pixels in size. So, how about using two spaces instead of just one space for each pixel? That way you could retain the image's aspect ratio.
Oh, and some of the intermediate PPM images may contain comments. Sometimes I get this:
./siv.sh: line 116: #AppleMark: syntax error: operand expected (error token is "#AppleMark")Offline
Thank you, this is a great idea for a program. (especially an Arch program) ![]()
I agree with Vain that an option to make each "pixel" two columns wide would be beneficial.
Also, will you please add it to the AUR?
Offline
Hijacking my own thread... (-;
Vain implemented the script with awk and now its much faster.
Look at: http://gist.github.com/612099#
Offline
What do you guys think about http://xyne.archlinux.ca/projects/tiv ? It's basically the same I guess
Offline
Hijacking my own thread... (-;
Vain implemented the script with awk and now its much faster.
Look at: http://gist.github.com/612099#
I was going to do that. ![]()
Nice original idea though, and Vain's saved me the effort, I suppose. ![]()
tiv does look the same but great minds and all that. ![]()
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
What do you guys think about http://xyne.archlinux.ca/projects/tiv ? It's basically the same I guess
Good to know but I'll never be a perl friend I think. ![]()
Offline
What do you guys think about http://xyne.archlinux.ca/projects/tiv ? It's basically the same I guess
Right. Forgot about that one – now that you mention it… ![]()
Hmm, tiv looks better. Especially when it comes to handling of aspect-ratio. So I revised that part. It now resizes the image properly instead of simply using two blanks… It should almost look like "tiv -r 0.5 …" now.
Offline
Isn't feh small?
Offline
Isn't feh small?
If you're using imagemagick anyway, then it'd be obvious to use display - not sure which is smaller, what with dependencies and the like.
However, I thought this was to use outside of X....though then 256 colour is not so useful (I've just tried it in fbterm and could only get 8 colours to work).
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Pages: 1