简体   繁体   中英

Getting _curses.error: curses function returned NULL in windows-curses

I checked other answers but I had no luck.

So I'm doing a thing with open-cv and the terminal with windows-curses but I'm getting the _curses.error: curses function returned NULL error

I'm still a noobie with curses, therefore I have ZERO clue about why this happens

Here's the code

from string import ascii_letters
import cv2 as cv
import numpy as np
import os
import curses as cs

basefolder = os.path.dirname(os.path.abspath(__file__)).replace(os.path.basename(__file__), '') + '\\'

def map_range(value, leftMin, leftMax, rightMin, rightMax):
    # Figure out how 'wide' each range is
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin

    # Convert the left range into a 0-1 range (float)
    valueScaled = float(value - leftMin) / float(leftSpan)

    # Convert the 0-1 range into a value in the right range.
    return rightMin + (valueScaled * rightSpan)

def asciify(matrix):
    w, h = len(matrix[0]), len(matrix)
    
    asciis = '$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`\'.'
    output = np.empty(shape=(h, w), dtype=str)

    for i in range(h):
        for j in range(w):
            asc = map_range(matrix[i, j], 255, 0, len(asciis)-1, 0)
            output[i, j] = asciis[int(asc)]





    return output


def main(stdscr):

    cs.initscr()

    img = cv.imread(basefolder + 'cat.jpg', 0)
    ascii_img = asciify(img)

    w, h = len(ascii_img[0]), len(ascii_img)

    win = cs.newwin(h, w, 0, 0)

    win.clear()
    
    for i in range(h):
        strg = []
        for j in range(w):
            strg.append(ascii_img[i, j])
        
        win.addstr(i, 0, ''.join(strg))


    win.refresh()
    stdscr.getch()

cs.wrapper(main)

On Windows I was able to get past the "_curses.error: curses function returned NULL error" by setting the environment variables for LINES and COLS. In my case this was added to the runtime environment in pycharm under "Environment variables". For example: LINES=40 COLS=80

If you are working in linux you can achieve the same result with:

export LINES=40

export COLS=80

Set the values to something that works for your display - it is important that is scaled to fit the entire range that you are using so in your example it looks like you would need an area that handles the height and width of the matrix.

I have been having somewhat limited success with the windows-curses package. I can get parts of it to work (newwin) and not others (newpad). Works fine on Linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM