Python实现点阵字体读取与转换
点阵字体是指根据文字的像素点来显示的字体,效果如下:
使用Python读取并显示的过程如下:
- 根据中文字符获取GB2312编码
- 通过GB2312编码计算该汉字在点阵字库中的区位和码位
- 通过区位和码位计算在点阵字库中的偏移量
- 基于偏移量获取该汉字的32个像素存储字节
- 解析像素字节获取点阵坐标信息
- 在对应的坐标显示信息位。如该像素点是否显示点亮
使用该代码前提:下载点阵字体库到本地,这里默认使用的是hzk16点阵字库
字体库下载地址:
代码如下:
#!/usr/bin/python
#encoding: utf-8
import binasciiRECT_HEIGHT = 16
RECT_WIDTH = 16
BYTE_COUNT_PER_ROW = RECT_WIDTH / 8
BYTE_COUNT_PER_FONT = BYTE_COUNT_PER_ROW * RECT_HEIGHTKEYS = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]class FontRender(object):def __init__(self, font_file,rect_height=RECT_HEIGHT, rect_width=RECT_WIDTH, byte_count_per_row=BYTE_COUNT_PER_ROW):self.font_file = font_fileself.rect_height = rect_heightself.rect_width = rect_widthself.byte_count_per_row = byte_count_per_rowself.__init_rect_list__()def __init_rect_list__(self):self.rect_list = [] * RECT_HEIGHTfor i in range(RECT_HEIGHT):self.rect_list.append([] * RECT_WIDTH)def get_font_area_index(self, txt, encoding='utf-8'):if not isinstance(txt, unicode):txt = txt.decode(encoding)gb2312 = txt.encode('gb2312')hex_str = binascii.b2a_hex(gb2312)area = eval('0x' + hex_str[:2]) - 0xA0index = eval('0x' + hex_str[2:]) - 0xA0return area, indexdef get_font_rect(self, area, index):offset = (94 * (area-1) + (index-1)) * BYTE_COUNT_PER_FONTbtxt = Nonewith open(self.font_file, "rb") as f:f.seek(offset)btxt = f.read(BYTE_COUNT_PER_FONT)return btxtdef convert_font_rect(self, font_rect, ft=1, ff=0):for k in range(len(font_rect) / self.byte_count_per_row):row_list = self.rect_list[k]for j in range(self.byte_count_per_row):for i in range(8):asc = binascii.b2a_hex(font_rect[k * self.byte_count_per_row + j])asc = eval('0x' + asc)flag = asc & KEYS[i]row_list.append(flag and ft or ff)def render_font_rect(self, rect_list=None):if not rect_list:rect_list = self.rect_listfor row in rect_list:for i in row:if i:print '■',else:print '○',printdef convert(self, text, ft=None, ff=None, encoding='utf-8'):if not isinstance(text, unicode):text = text.decode(encoding)for t in text:area, index = self.get_font_area_index(t)font_rect = self.get_font_rect(area, index)self.convert_font_rect(font_rect, ft=ft, ff=ff)def get_rect_info(self):return self.rect_listif '__main__' == __name__:text = u'同创伟业'fr = FontRender('./font/16x16/hzk16h')fr.convert(text, ft='/static/*', ff=0)# print fr.get_rect_info()fr.render_font_rect()
关于学习Python的更多文章,请扫描下方二维码。
Python实现点阵字体读取与转换相关推荐
- python获取中文字体点阵坐标_Python实现点阵字体读取与转换的方法
点阵字体是指根据文字的像素点来显示的字体,效果如下: 使用Python读取并显示的过程如下: 根据中文字符获取GB2312编码 通过GB2312编码计算该汉字在点阵字库中的区位和码位 通过区位和码位计 ...
- python点阵显示数字_Python实现点阵字体读取与转换的方法
点阵字体是指根据文字的像素点来显示的字体,效果如下: 使用python读取并显示的过程如下: 根据中文字符获取GB2312编码 通过GB2312编码计算该汉字在点阵字库中的区位和码位 通过区位和码位计 ...
- python获取中文字体点阵坐标_Python实现点阵字体读取与转换
点阵字体是指根据文字的像素点来显示的字体,效果如下: 使用Python读取并显示的过程如下: 根据中文字符获取GB2312编码 通过GB2312编码计算该汉字在点阵字库中的区位和码位 通过区位和码位计 ...
- python、PyTorch图像读取与numpy转换
原文:https://blog.csdn.net/yskyskyer123/article/details/80707038 python.PyTorch图像读取与numpy转换 Tensor转为nu ...
- json文件读取并转换成为字典python
json文件读取并转换成为字典python # JSON到字典转化 f = open('info.json', 'r') info_data = json.load(f) print(info_dat ...
- python如何读取二进制文件为图片_Python二进制文件读取并转换
Python二进制文件读取并转换 Python二进制文件读取并转换 标签(空格分隔): python 本文所用环境: Python 3.6.5 |Anaconda custom (64-bit)| 引 ...
- python将视频按帧读取并转换成图片
python将视频按帧读取并转换成图片 代码如下 from glob import glob import cv2 import tqdm import os video_path = glob(r' ...
- 【STM32F429开发板用户手册】第42章 STM32F429的LTDC应用之点阵字体和字符编码(重要)
最新教程下载:http://www.armbbs.cn/forum.php?mod=viewthread&tid=93255 第42章 STM32F429的LTDC应用之点阵字体和 ...
- 【STM32H7】第17章 ThreadX GUIX之点阵字体和字符编码(重要)
最新教程下载:http://www.armbbs.cn/forum.php?mod=viewthread&tid=98429 第17章 ThreadX GUIX之点阵字体和字符编码 ...
最新文章
- 计算机常见知识获取方法,计算机信息处理技术基础知识
- 【PL/SQL】学习笔记 (7)光标的属性,一个会话中打开光标数的限制
- Python面向对象编程(二)
- js用button激活 Alert 元素关闭按钮的交互功能
- 管理科学の学生自学CS的【计划】与【心得】
- Webservice检查
- linux中目录的作用是什么,在linux文件系统中,/etc 目录的作用到底是干什么用的?...
- SpringMVC 参数绑定注解解析
- CSS Reset Modern CSS Reset
- STM32F103C8T6开发环境的搭建
- 【githubshare】开源的小说下载与阅读工具:Uncle 小说。目录解析与书源结合,支持有声小说与文本小说,可下载 mobi、epub、txt 格式文本小说
- 【论文翻译】Mastering the game of Go with deep neural networks and tree search( 用深度神经网络和树搜索实现围棋游戏)
- Only one connection receive subscriber allowed.
- Simulink仿真计算中保留特定位数的小数
- 批量图像自动分割 grabcut+阈值分割 opencv
- win10无法装载iso文件_iso镜像安装失败无法安装Windows10怎么办?
- 移动端click事件无反应或反应慢 touchend事件页面滑动时频繁触发
- http://www.youku.com/playlist_show/id_4637211.html
- Linux强制使用短密码|修改短密码|passwd无效的密码
- Debian11(Bullseye)系统安装docker及启动失败问题解决