You are not logged in.

#1 2010-10-05 14:13:12

echasslau
Member
Registered: 2007-09-21
Posts: 113

Shell image viewer

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
done

usage:

$ ./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

#2 2010-10-05 14:21:56

livibetter
Member
From: Taipei
Registered: 2008-05-14
Posts: 95
Website

Re: Shell image viewer

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)  # blue

before line 95.

Offline

#3 2010-10-05 14:25:11

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Shell image viewer

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)

Offline

#4 2010-10-05 14:26:33

echasslau
Member
Registered: 2007-09-21
Posts: 113

Re: Shell image viewer

ok, must be a copy&paste error
fixed it in the first post

Offline

#5 2010-10-05 14:31:15

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Shell image viewer

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.

Last edited by karol (2010-10-05 14:31:53)

Offline

#6 2010-10-05 14:46:12

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Shell image viewer

karol wrote:
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.

Offline

#7 2010-10-05 15:18:22

echasslau
Member
Registered: 2007-09-21
Posts: 113

Re: Shell image viewer

Thanks for the advices. Updated...
No massive changes but now it doesn't need any tmp file.

Offline

#8 2010-10-05 16:28:30

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Shell image viewer

brisbin33 wrote:

since you're bashy anyway, use [[ and ]], because they're faster than [ and ].

I didn't know that, thanks for bringing it up.

Offline

#9 2010-10-05 16:29:39

Vain
Member
Registered: 2008-10-19
Posts: 183

Re: Shell image viewer

That's very sexy. smile

However, at least on my machines, it's not very fast. I can watch every single line appear on the screen. smile (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

#10 2010-10-05 18:04:54

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Shell image viewer

Thank you, this is a great idea for a program. (especially an Arch program) tongue

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

#11 2010-10-05 19:39:18

echasslau
Member
Registered: 2007-09-21
Posts: 113

Re: Shell image viewer

Hijacking my own thread... (-;

Vain implemented the script with awk and now its much faster.
Look at: http://gist.github.com/612099#

Offline

#12 2010-10-05 20:19:43

Army
Member
Registered: 2007-12-07
Posts: 1,784

Re: Shell image viewer

What do you guys think about http://xyne.archlinux.ca/projects/tiv ? It's basically the same I guess

Offline

#13 2010-10-05 20:24:42

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Shell image viewer

echasslau wrote:

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. neutral
Nice original idea though, and Vain's saved me the effort, I suppose. wink

tiv does look the same but great minds and all that. smile


"...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

#14 2010-10-05 20:28:06

echasslau
Member
Registered: 2007-09-21
Posts: 113

Re: Shell image viewer

Army wrote:

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. smile

Offline

#15 2010-10-05 22:35:52

Vain
Member
Registered: 2008-10-19
Posts: 183

Re: Shell image viewer

Army wrote:

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… wink

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

#16 2010-10-06 03:29:28

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Re: Shell image viewer

Isn't feh small?

Offline

#17 2010-10-06 08:41:01

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Shell image viewer

hatten wrote:

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

Board footer

Powered by FluxBB