Bootstrap

少帅下飞机视频转ASCII动画(python+opencv)

话不多说先上图:

实现图中效果的步骤如下:

        1.视频转帧图(Video to Frames)

        2.帧图转字符画(Frames to ASCII)

       3.字符画转图片(ASCII to imgs)

        4.图片合成视频 (imgs to ASCII Video)

1.视频转帧图(Video to Frames)

安装opencv 

pip install opencv-python

视频抽帧成一张张图片:

def video_to_frames(video_path, output_folder, frame_interval=1):
    """
    将视频抽帧并保存为图片文件。

    :param video_path: 视频文件的路径。
    :param output_folder: 保存图片帧的文件夹路径。
    :param frame_interval: 每隔多少帧抽取一帧,默认为1(即抽取每一帧)。
    """
    # 打开视频文件
    cap = cv2.VideoCapture(video_path)

    if not cap.isOpened():
        raise IOError("无法打开视频文件")

    frame_count = 0
    saved_frame_count = 0
    while True:
        ret, frame = cap.read()
        if not ret:
            break  

        if frame_count % frame_interval == 0:
            # 保存帧
            output_path = os.path.join(output_folder, f"frame_{saved_frame_count:04d}.jpg")
            cv2.imwrite(output_path, frame)
            saved_frame_count += 1

        frame_count += 1

    cap.release()

2.帧图转字符画(Frames to ASCII)

def image_to_colored_ascii(image_path, output_width, output_height):
    resized_image = resize_image(image_path, output_width, output_height)

    ascii_image = []
    ascii_image_chars = []
    ascii_image_colors=[]
    for y in range(output_height):
        row = ""
        for x in range(output_width):
            color = resized_image.getpixel((x, y))

            gray_value = sum(color) // 3
            char_index = int(gray_value / 255 * (len(colored_ascii_chars) - 1))
            if char_index >= len(colored_ascii_chars):
                char_index = len(colored_ascii_chars) - 1

            row += f"\033[38;2;{color[0]};{color[1]};{color[2]}m{colored_ascii_chars[char_index]}"

            ascii_image_chars.append(colored_ascii_chars[char_index])
            ascii_image_colors.append((color[0], color[1], color[2]))

        ascii_image.append(row)

    return "\n".join(ascii_image), ascii_image_chars, ascii_image_colors

3.字符画转图片(ASCII to imgs)

# 将 ASCII 艺术图像存储为图片
def save_colored_ascii_image_as_picture(ascii_image_chars, ascii_image_colors, font_path, font_size, output_path):
    image = Image.new('RGB', ((output_width * font_size), (output_height * font_size)), color='black')
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype(font_path, font_size)

    for i in range(output_height):
        for j in range(output_width):
            draw.text((j * font_size, i * font_size), ascii_image_chars[i*output_width+j], font=font, fill=ascii_image_colors[i*output_width+j])

    image.save(output_path)

4.图片合成视频 (imgs to ASCII Video)

def process_images_in_directory(input_dir, output_dir, output_width, output_height, font_path, font_size):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # 支持的图片格式列表
    supported_extensions = ('.jpg', '.jpeg', '.png', '.bmp', '.gif')

    index = 0

    # 遍历目录中的所有文件
    for filename in os.listdir(input_dir):
        if filename.lower().endswith(supported_extensions):
            input_path = os.path.join(input_dir, filename)

            output_path = os.path.join(output_dir, "ss_00" + str(index) + ".jpg")
            index += 1
            # 将图像转换为彩色ASCII字符画
            colored_ascii_image, colored_ascii_image_chars, colored_ascii_image_colors = image_to_colored_ascii(input_path, output_width, output_height)

            # 存储彩色ASCII字符画到图片文件
            save_colored_ascii_image_as_picture(colored_ascii_image_chars, colored_ascii_image_colors, font_path, font_size, output_path)

结尾tips:OpenCV读取的文件路径中不能带有中文字符,否则会报错:

完结。。。

参考 Python 制作 ASCII 字符画 

;