terminal-image-viewer icon indicating copy to clipboard operation
terminal-image-viewer copied to clipboard

NO WORKS

Open blixer-debug opened this issue 4 years ago • 3 comments

File "img-viewer.py", line 36 print(get_color(pix[0], pix[1], pix[2]), sep='', end='') ^ SyntaxError: invalid syntax

blixer-debug avatar Nov 24 '20 09:11 blixer-debug

My OS: UBUNTU 20.04. Terminal: Cool-retro-term Python:Updated

blixer-debug avatar Nov 24 '20 09:11 blixer-debug

python3 img-viewer.py <image file name>

assasinfil avatar Dec 17 '20 19:12 assasinfil

I had the same issue, but I'm on different locked down old distro that doesn't have Python 3 as an option.

I did solve it via https://stackoverflow.com/a/26922310/13312932 : pip install future ... then added to file at top from __future__ import print_function

also, as side effect of trying to make it work I've found these suggestions for improvement as well - https://stackoverflow.com/a/54790086/13312932 : img = Image.open(img_path).convert('RGB') and w = int((img.width / img.height) * h) * 2

Full code:

from __future__ import print_function
import sys
import numpy as np
from PIL import Image

def get_ansi_color_code(r, g, b):
    if r == g and g == b:
        if r < 8:
            return 16
        if r > 248:
            return 231
        return round(((r - 8) / 247) * 24) + 232
    return 16 + (36 * round(r / 255 * 5)) + (6 * round(g / 255 * 5)) + round(b / 255 * 5)

def get_color(r, g, b):
    return "\x1b[48;5;{}m \x1b[0m".format(int(get_ansi_color_code(r,g,b)))

def show_image(img_path):
        try:
                img = Image.open(img_path).convert('RGB')
        except FileNotFoundError:
                exit('Image not found.')

        h = 100
        w = int((img.width / img.height) * h) * 2

        img = img.resize((w,h), Image.ANTIALIAS)
        img_arr = np.asarray(img)
        h,w,c = img_arr.shape

        for x in range(h):
            for y in range(w):
                pix = img_arr[x][y]
                print(get_color(pix[0], pix[1], pix[2]), sep='', end='')
            print()

if __name__ == '__main__':
        if len(sys.argv) > 1:
                img_path = sys.argv[1]
                show_image(img_path)


I get a pretty good result, it is not as good as in YouTube video, but it's at least partial representation of the image, thank you!

luxzg avatar Jun 10 '21 14:06 luxzg