@TOC
前言
少帅下飞机 视频转 ascii 艺术字 python 实现 源码复制直接运行
代码
import cv2
import time
import os
import pickle
from blessed import Terminal
import numpy as np
# 初始化 blessed 的终端对象
term = Terminal()
# 更复杂的字符集,用于增强灰度映射效果
ASCII_CHARS = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
# 将每个像素映射为ASCII字符
def pixel_to_ascii(pixel_value):
ascii_index = np.uint16(pixel_value) * len(ASCII_CHARS) // 256
return ASCII_CHARS[min(ascii_index, len(ASCII_CHARS) - 1)]
# 将图像转换为ASCII艺术字符,并保留颜色信息
def rgb_to_ansi(r, g, b):
return f'\033[38;2;{
r};{
g};{
b}m'
def image_to_ascii_colored(frame, width=120):
height, orig_width, _ = frame.shape
aspect_ratio = height / orig_width
new_width = width
new_height = int(aspect_ratio * new_width * 0.43)
resized_frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_AREA)
gray_frame = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2GRAY)

最低0.47元/天 解锁文章
865

被折叠的 条评论
为什么被折叠?



