Python输出汉字字库及将文字转换为图片的方法

翟塘峡口曲江头,万里风烟接素秋。那遍地的绿草,那微风中轻轻摇曳的芦苇,那栖满了夜鹭的灌木丛,就像一幅立体的田园画,静静地展现在我的眼前。

用python输出汉字字库
问题1:假设我们知道汉字编码范围是0x4E00到0x9FA5,怎么从十六进制的编码转成人类可读的字呢?
问题2:怎么把unicode编码的字写入文件呢,如果直接用open()的话,会提示UnicodeEncodeError: 'ascii' codec can't encode character u'\u4e00' in position 0: ordinal not in range(128)

问题1的答案是用unichr,问题2的答案是用codecs。
下面上代码。

import codecs 
start,end = (0x4E00, 0x9FA5) 
with codecs.open("chinese.txt", "wb", encoding="utf-8") as f: 
 for codepoint in range(int(start),int(end)): 
  f.write(unichr(codepoint)) 

打开chinese.txt文件,截图如下

用python将文本转图片字库
上面提到怎么得到汉字字库,下面就来讲怎么把一个一个的字转成图片,这在机器学习中会有用处。
一句话,用pygame渲染文字到图片上。
下面上代码。

import os 
import pygame 
chinese_dir = 'chinese' 
if not os.path.exists(chinese_dir): 
 os.mkdir(chinese_dir) 
 
pygame.init() 
start,end = (0x4E00, 0x9FA5)#汉字编码范围 
for codepoint in range(int(start),int(end)): 
 word = unichr(codepoint) 
 font = pygame.font.Font("msyh.ttc", 22)#当前目录下要有微软雅黑的字体文件msyh.ttc,或者去c:\Windows\Fonts目录下找 
 rtext = font.render(word, True, (0, 0, 0), (255, 255, 255)) 
 pygame.image.save(rtext, os.path.join(chinese_dir,word+".png")) 

下面是效果截图。

以上就是Python输出汉字字库及将文字转换为图片的方法。高标准,精细化,零缺陷。更多关于Python输出汉字字库及将文字转换为图片的方法请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python-时间转换为时间戳

使用Python将字符串转换为格式化的日期时间字符串

Python将主机名转换为IP地址的方法

详解将Python程序(.py)转换为Windows可执行文件(.exe)

python实现批量nii文件转换为png图像