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

时间:2022-09-15 13:50:04

用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。
下面上代码。

?
1
2
3
4
5
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输出汉字字库及将文字转换为图片的方法

 

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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输出汉字字库及将文字转换为图片的方法