摘要
本文为利用python代码实现从视频到点阵的快速生成,方便为单片机的动画提供点阵资源。从视频获取连续帧图使用的是ffmpeg。相应的资源文件在附件提供。
版本信息:python-3.10,selenium-4.9.0,PIL-Image-9.4.0.
样例
根据封面生成的128x64的点阵, light_ratio=200
代码
# -*- coding: utf-8 -*-
# @Time : 2024/4/14 15:01
import os
import time
import subprocess
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
class GenerateFontBitmapFromVideo:
def __init__(self, video_path, frames_dir_path, drop_ratio, row_width, column_height, reverse):
self.video_path = video_path # 视频文件路径
self.frames_dir_path = frames_dir_path # 输出帧图片的目录和文件名格式
self.drop_ratio = drop_ratio
self.row_width = row_width
self.column_height = column_height
self.reverse = reverse
def generateFramesFromVideo(self, start_time="00:00:00", duration="10"):
output_pattern = os.path.join(self.frames_dir_path, 'frame_%04d.png')
# 构建FFmpeg命令
ffmpeg_command = [
'ffmpeg',
'-i', self.video_path,
'-ss', start_time,
'-t', duration,
output_pattern
]
# 执行FFmpeg命令
subprocess.run(ffmpeg_command)
def generateBitmapFromZheTao(self, driver):
driver.get("https://www.zhetao.com/fontarray.html")
time.sleep(1)
rows = driver.find_element(By.ID, "rows")
coltimes = driver.find_element(By.ID, "coltimes")
inputs = driver.find_element(By.ID, "selfile")
text = driver.find_element(By.ID, "chars")
buttons = driver.find_elements(By.CLASS_NAME, "genbtn")
store = open("store.txt", "w")
store.write("u8 frames[][{}*{}]".format(self.row_width // 8, self.column_height) + " = {\n")
def extract_and_save(content, index):
# 寻找起始行的索引
start_marker = "static const unsigned char bitmap_bit_bytes[] = "
end_marker = "};"
start_index = content.find(start_marker)
if start_index == -1:
print("Start marker not found.")
return
start_index += len(start_marker)
# 寻找结束行的索引
end_index = content.find(end_marker, start_index)
if end_index == -1:
print("End marker not found.")
return
# 加上结束标记长度,确保包括结束行