本文章向大家介紹使用Python怎么對(duì)點(diǎn)陣字體進(jìn)行讀取,主要包括使用Python怎么對(duì)點(diǎn)陣字體進(jìn)行讀取的使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。

Python是一種編程語(yǔ)言,內(nèi)置了許多有效的工具,Python幾乎無(wú)所不能,該語(yǔ)言通俗易懂、容易入門(mén)、功能強(qiáng)大,在許多領(lǐng)域中都有廣泛的應(yīng)用,例如最熱門(mén)的大數(shù)據(jù)分析,人工智能,Web開(kāi)發(fā)等。
使用Python讀取并顯示的過(guò)程如下:
根據(jù)中文字符獲取GB2312編碼
通過(guò)GB2312編碼計(jì)算該漢字在點(diǎn)陣字庫(kù)中的區(qū)位和碼位
通過(guò)區(qū)位和碼位計(jì)算在點(diǎn)陣字庫(kù)中的偏移量
基于偏移量獲取該漢字的32個(gè)像素存儲(chǔ)字節(jié)
解析像素字節(jié)獲取點(diǎn)陣坐標(biāo)信息
在對(duì)應(yīng)的坐標(biāo)顯示信息位。如該像素點(diǎn)是否顯示點(diǎn)亮
使用該代碼前提:下載點(diǎn)陣字體庫(kù)到本地,這里默認(rèn)使用的是hzk16點(diǎn)陣字庫(kù)
代碼如下:
#!/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()到此這篇關(guān)于使用Python怎么對(duì)點(diǎn)陣字體進(jìn)行讀取的文章就介紹到這了,更多相關(guān)的內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!
當(dāng)前題目:使用Python怎么對(duì)點(diǎn)陣字體進(jìn)行讀取-創(chuàng)新互聯(lián)
本文URL:http://chinadenli.net/article18/gsigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、軟件開(kāi)發(fā)、外貿(mào)建站、網(wǎng)站收錄、服務(wù)器托管、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)