点阵快速生成方法与font6x8点阵分享

摘要

        本文为利用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

            # 加上结束标记长度,确保包括结束行
          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值