Python實(shí)現(xiàn)點(diǎn)陣字體讀取與轉(zhuǎn)換的方法
點(diǎn)陣字體是指根據(jù)文字的像素點(diǎn)來顯示的字體,效果如下:
使用Python讀取并顯示的過程如下:
根據(jù)中文字符獲取GB2312編碼
通過GB2312編碼計(jì)算該漢字在點(diǎn)陣字庫中的區(qū)位和碼位
通過區(qū)位和碼位計(jì)算在點(diǎn)陣字庫中的偏移量
基于偏移量獲取該漢字的32個像素存儲字節(jié)
解析像素字節(jié)獲取點(diǎn)陣坐標(biāo)信息
在對應(yīng)的坐標(biāo)顯示信息位。如該像素點(diǎn)是否顯示點(diǎn)亮
使用該代碼前提:下載點(diǎn)陣字體庫到本地,這里默認(rèn)使用的是hzk16點(diǎn)陣字庫
代碼如下:
#!/usr/bin/python #encoding: utf-8 import binascii RECT_HEIGHT = 16 RECT_WIDTH = 16 BYTE_COUNT_PER_ROW = RECT_WIDTH / 8 BYTE_COUNT_PER_FONT = BYTE_COUNT_PER_ROW * RECT_HEIGHT KEYS = [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_file self.rect_height = rect_height self.rect_width = rect_width self.byte_count_per_row = byte_count_per_row self.__init_rect_list__() def __init_rect_list__(self): self.rect_list = [] * RECT_HEIGHT for 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]) - 0xA0 index = eval('0x' + hex_str[2:]) - 0xA0 return area, index def get_font_rect(self, area, index): offset = (94 * (area-1) + (index-1)) * BYTE_COUNT_PER_FONT btxt = None with open(self.font_file, "rb") as f: f.seek(offset) btxt = f.read(BYTE_COUNT_PER_FONT) return btxt def 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_list for row in rect_list: for i in row: if i: print '■', else: print '○', print def 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_list if '__main__' == __name__: text = u'同創(chuàng)偉業(yè)' fr = FontRender('./font/16x16/hzk16h') fr.convert(text, ft='/static/*', ff=0) # print fr.get_rect_info() fr.render_font_rect()
以上這篇Python實(shí)現(xiàn)點(diǎn)陣字體讀取與轉(zhuǎn)換的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python+wxPython實(shí)現(xiàn)將圖片轉(zhuǎn)換為草圖
將照片轉(zhuǎn)換為藝術(shù)風(fēng)格的草圖是一種有趣的方式,可以為您的圖像添加獨(dú)特的效果,本文主要介紹了如何Python和wxPython來實(shí)現(xiàn)這一目標(biāo),需要的可以參考下2023-08-08python3 http提交json參數(shù)并獲取返回值的方法
今天小編就為大家分享一篇python3 http提交json參數(shù)并獲取返回值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python如何實(shí)現(xiàn)內(nèi)容寫在圖片上
這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)內(nèi)容寫在圖片上,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03Python中str.format()和f-string的使用
本文主要介紹了Python中str.format()和f-string的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02