Python脚本,用于将网络摄像头图像转换为cmd中的ASCII,而不会打印unicode

2024-04-19 08:47:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现这个python脚本可以将网络摄像头的输入转换成ASCII格式,并在命令行中进行了一些修改以满足我的需要。我想在输出中添加一个Unicode字符,但它目前只打印出一个框,而不是在我实际运行时打印出正确的Unicode字符。我需要更改什么以确保它打印出实际的Unicode字符?你知道吗

import cv2
from time import sleep, time
import sys

import curses
from curses import wrapper

import locale
locale.setlocale(locale.LC_ALL, '')
#stdscr.addstr(0, 0, mystring.encode('UTF-8'))

x = 160
sx = 4

chars = ['\u23FA', ".", ".", "  ", "  ", "  ", "  ", "  ", "  ", "  "]

size = x*sx, int(x*sx*.75)
operating = False
ongoing = True
#font.set_bold(True)
fpsa = 0

br = (255, 255, 255)
fr = (0, 0, 0)

lenc = len(chars)

visload = False
mt = False
aa = False

def toAscii(pic, scr):
    global operating
    m = 0
    for y in pic:
        tm = max(y)
        if tm > m:
            m = tm

    fx = 0
    fy = 0

    h,w = scr.getmaxyx()

    #for y in pic:
        #for x in y:
    for _y in range(h-1):
        for _x in range(w-1):
            y = pic[int(_y/float(h) * len(pic))]
            x = y[int(_x/float(w) * len(y))]
            scr.addstr(_y, _x, chars[int(x/m*(lenc-1))].encode('UTF-8'), curses.color_pair(1))
            fx += 1
        fy += 1
        fx = 0
    operating = False


cap = cv2.VideoCapture(0)

def main(scr):

    global operating

    while True:
        ret, frame = cap.read()

        curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)

        colored = cv2.resize(cv2.resize(cv2.cvtColor(frame, 0), (x, int(x*0.75))), (640, 480))
        colored = cv2.cvtColor(frame, 0)


        gray = cv2.resize(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), (x, int(x*.75)))

        if not operating:
            operating = True
            if mt:
                _thread.start_new_thread(toAscii, (gray, scr))
            else:
                toAscii(gray, scr)

        scr.refresh()


        #gray = cv2.resize(gray, (640, 480), interpolation = cv2.INTER_NEAREST)


        #cv2.imshow('frame',cv2.resize(gray, (640, 480)))

def _main(scr):
    try:
        main(scr)
    except KeyboardInterrupt:
        pass

wrapper(_main)

cap.release()
cv2.destroyAllWindows()

Tags: inimportfalsetrueformainoperatingcv2
1条回答
网友
1楼 · 发布于 2024-04-19 08:47:08

这取决于您使用的终端仿真器。emoji字符在终端中显示为方框的原因是终端仿真器不支持Unicode字符。你知道吗

我刚刚在iTerm2上测试了您的代码,它支持Unicode,并且可以正常工作。你知道吗

相关问题 更多 >