Search code examples
pythoncurses

How to turn representation of 8-bit colors to corresponding python curses colors?


I am trying to show low resolution image in my terminal app build in python curses. I was getting rgb color of every pixel of image using numpy. I was changing every pixel to corresponding 8-bit color using this: color = int((r*6/256)*36 + (g*6/256)*6 + (b*6/256) - 1) and using color as new init_pair, everything worked okay except colors, they were not even similar. So my question is: is there any way to turn representation of 8-bit color to corresponding one of 256 python curses colors?

There is part of my code:

def show_image(h, w, img_arr, window):
    for y in range(h):
        for x in range(w):
            pix = img_arr[y][x]
            color = int((pix[0]*6/256)*36 + (pix[1]*6/256)*6 + (pix[2]*6/256) - 1)
            curses.init_pair(color, color, color)
            window.addstr(10+y, x+1, "#", curses.color_pair(color))

Solution

  • You'd have to tell curses what the colors are, e.g., using init_color (and that only works with a terminal that handles color-initialization, using a terminal description that has initc). Also, using the color value as the pair-number in init_pair probably will not work as you intend.