天天看点

python工具方法 13 win系统字库保存为图片文件一、字体库存储路径二、字体库提取保存代码 三、保存效果

将系统中的字体保存为图片,为orc提供训练数据

一、字体库存储路径

win10系统字库文件所在的目录为c:\Windows\Fonts\

python工具方法 13 win系统字库保存为图片文件一、字体库存储路径二、字体库提取保存代码 三、保存效果

二、字体库提取保存代码 

代码中选择的字体库是Microsoft YaHei的,字库保存的代码如下

#encoding: utf-8
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 = chr(codepoint)#求十进制或十六进制对应的字符
    print(word)
    #font = pygame.font.SysFont('Microsoft YaHei', 64)
    font = pygame.font.Font("c:\Windows\Fonts\msyh.ttc", 64)
    ftext = font.render(word, True, (0,0,0),(255, 255, 255))
    pygame.image.save(ftext, (chinese_dir+ str(codepoint) + ".png"))
           

三、保存效果

python工具方法 13 win系统字库保存为图片文件一、字体库存储路径二、字体库提取保存代码 三、保存效果

继续阅读