Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: [EN] terminal image view script  (Read 4336 times)

Offline se7en

  • User
  • Posts: 110
[EN] terminal image view script
« on: 2012/07/02, 11:29:45 »
I found this script to view images without X

It needs imagemagik and python-imaging installed.
You might also have to adjust the #!/ to the version of python you are using.

Also if
Code: [Select]
tput colors returnes NOT 256 add
Code: [Select]
export TERM="xterm-256color"
to your .bashrc and reload it via
Code: [Select]
. .bashrc

The script: image.sh
Code: [Select]
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import Image
import sys
import getopt
import time

bash = False
width = 0
height = 0
imgWidth = 0
imgHeight = 0
character = u'▄'
verbose = False
native = "_xterm256.c"

CUBE_STEPS = [0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF]
BASIC16 = ((0, 0, 0), (205, 0, 0), (0, 205, 0), (205, 205, 0),
           (0, 0, 238), (205, 0, 205), (0, 205, 205), (229, 229, 229),
           (127, 127, 127), (255, 0, 0), (0, 255, 0), (255, 255, 0),
           (92, 92, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255))

def usage():
    print("\n  "+sys.argv[0]+" [-whcbvn] image")
    print("  Options:")
    print("  -w --width          - width in pixels, if specified without height, the image")
    print("                        will be scaled proportionally")
    print("  -h --height         - see width")
    print("  -c --character      - character to use for foreground")
    print("  -b --as-bash-script - output is a bash script")
    print("  -v --verbose        - verbose output")
    print("  -n --native         - name of the C-file which will be used to replace xterm_to_rgb")
    print("                        and rgb_to_xterm by native methods")
    print("  --help              - this")
    print("  image               - Any image or gif (output will be animated)\n")
   
def xterm_to_rgb(xcolor):
    assert 0 <= xcolor <= 255
    if xcolor < 16:
        # basic colors
        return BASIC16[xcolor]
    elif 16 <= xcolor <= 231:
        # color cube
        xcolor -= 16
        return (CUBE_STEPS[(xcolor / 36) % 6],
                CUBE_STEPS[(xcolor / 6) % 6],
                CUBE_STEPS[xcolor % 6])
    elif 232 <= xcolor <= 255:
        # gray tone
        c = 8 + (xcolor - 232) * 0x0A
        return (c, c, c)

COLOR_TABLE = [xterm_to_rgb(i) for i in xrange(256)]

def rgb_to_xterm(r, g, b):
    if r < 5 and g < 5 and b < 5:
        return 16
    best_match = 0
    smallest_distance = 10000000000
    for c in xrange(16, 256):
        d = (COLOR_TABLE[c][0] - r) ** 2 + \
            (COLOR_TABLE[c][1] - g) ** 2 + \
            (COLOR_TABLE[c][2] - b) ** 2
        if d <smallest_distance> getmtime(library):
        build = "gcc -fPIC -shared -o %s %s" % (library, sauce)
        assert os.system(build + " >/dev/null 2>&1") == 0
    xterm256_c = ctypes.cdll.LoadLibrary(library)
    xterm256_c.init()
    def xterm_to_rgb(xcolor):
        res = xterm256_c.xterm_to_rgb_i(xcolor)
        return ((res >> 16) & 0xFF, (res >> 8) & 0xFF, res & 0xFF)
    return (xterm256_c.rgb_to_xterm, xterm_to_rgb)
   
try:
    opts, args = getopt.getopt(sys.argv[1:], "w:h:c:n:bv",["width=","height=","character=","native=","as-bash-script","help","verbose"])
except:
    usage()
    sys.exit(1)

for o, a in opts:
    if o in ("-b", "as-bash-script"):
        bash = True
    elif o in ("-w", "--width"):
        width = int(a)
    elif o in ("-h", "--height"):
        height = int(a)
    elif o in ("-c", "--character"):
        character = a
    elif o in ("-v", "--verbose"):
        verbose = True
    elif o in ("-n", "--native"):
        native = a
    elif o in ("--help"):
        usage()
        sys.exit(0)

if(len(args)==0):
    print("No image given")
    usage()
    sys.exit(1)
im = Image.open(args[0])
imgWidth = im.size[0]
imgHeight = im.size[1]

try:
    (rgb_to_xterm, xterm_to_rgb) = compile_speedup()
except:
    if verbose and not bash:
        print("Failed to compile code, no speedup")

if width!=0 or height!=0:
    if width==0:
        width=int(imgWidth*(height/float(imgHeight)))
    if height==0:
        height=int(imgHeight*(width/float(imgWidth)))
else:
    width = imgWidth
    height = imgHeight
   
iterateImages(im)


image.sh -w100 image_file.png


Offline devil

  • Administrator
  • User
  • *****
  • Posts: 4.838
terminal image view script
« Reply #1 on: 2012/07/02, 13:44:25 »
Imagemagick brings it's own viewer as far as i remember.
Besides that there is the tools fbi and zgv dedicated to the job.

greetz
devil