本文为作者原创,未经允许,不得转载。
主要目的
本文主要是为了做印刷体字体识别的前期工作做准备,后期需要大量字体样本做神经网络训练,但缺乏印刷体样本,因此特地写了个程序自动生成所需要的样本。
##主要实现过程
本文主要分为三个部分实现,主要包括文本字库预处理,生成图片字已经保存到相应文件夹。其中文本字库采用的是百度上搜索到的《常用汉字3500个》,生成图片用的是PIL模块。
程序代码:
#文件目录一
# -*- coding: utf-8 -*-
#获取字体文件名,字体文件可以在百度下载或者电脑字体目录下寻找
import os
def Word_Font():
Word_Font_Path = \'D:\\pycharm11\\文字生成\\Word_Font\'
dirs = os.listdir(Word_Font_Path)
Word_Font_List=[]
for dir in dirs:
Word_Font_List.append(dir)
return Word_Font_List
#文件目录二
import re
f = open (\"D:\\pycharm11\\文字生成\\常用汉字3500个.txt\",\"r\")
lines = f.readlines() # 读取全部内容 ,并以列表方式返回
Library = []
for line in lines:
line = line.split(\'\\n\')
line = re.sub(r\'\\n\', \"\", line[0])
line = re.sub(\':\', \"\", line)
line=line.replace(\" \", \"\")
line = line.lstrip(\':\')
line = line[:0] + line[13:]
for i in line:
Library.append(i)
size_L=len(Library)
#主程序目录
from PIL import Image, ImageDraw, ImageFont, ImageOps
import os
import re
import Word_Font
#读取字体文件
Word_Font_List = Word_Font.Word_Font()
#选择字体以及图片参数的初始值
class LetterImage():
def __init__(self, imgSize=(0, 0), imgMode=\'RGB\', bg_color=(0, 0,0), fg_color=(255, 255, 255),
fontsize=10,Word_Font=Word_Font_List[1]):
self.imgSize = imgSize
self.imgMode = imgMode
self.fontsize = fontsize
self.bg_color = bg_color
self.fg_color = fg_color
self.font = ImageFont.truetype(Word_Font, fontsize)
#设定生成图片大小
def GenLetterImage(self, letters):
self.letters = letters
(self.letterWidth, self.letterHeight) = self.font.getsize(letters)
if self.imgSize == (0, 0):
self.imgSize = (self.letterWidth - 0, self.letterHeight +15) #底边边距
self.imgWidth, self.imgHeight = self.imgSize
self.img = Image.new(self.imgMode, self.imgSize, self.bg_color)
self.drawBrush = ImageDraw.Draw(self.img)
textY0 = (self.imgHeight - self.letterHeight-2 )
textY0 = int(textY0)
textX0 = int((self.imgWidth - self.letterWidth-2 )) #显示窗口坐标
self.drawBrush.text((textX0, textY0), self.letters, fill=self.fg_color, font=self.font)
if __name__ == \'__main__\':
f = open(\"D:\\pycharm11\\文字生成\\常用汉字3500个.txt\", \"r\")
lines = f.readlines() # 读取全部内容 ,并以列表方式返回
Library = []
for line in lines:
line = line.split(\'\\n\')
line = re.sub(r\'\\n\', \"\", line[0])
line = re.sub(\':\', \"\", line)
line = line.replace(\" \", \"\")
line = line.lstrip(\':\')
# line = line[:0] + line[13:]
for i in line:
Library.append(i)
letterList = []
#---------------将图片参数追加到列表以便后期调用--------------------
for j in range (0,len(Word_Font_List),1):
letterList.append(LetterImage(bg_color=(0, 120, 0), fontsize=100,Word_Font=Word_Font_List[j]))
print(Word_Font_List[j])
num_letter = len(Library) #字体数量
# ---------------------------创建文件夹------------------------------
File_name = re.sub(r\'\\.\', \'_\', Word_Font_List[j])
paths = os.getcwd()[:-4] + \'文字生成\\\\\'+File_name # 获取此py文件路径,在此路径选创建文件夹
if not os.path.exists(paths):
os.makedirs(paths)
paths = paths +\"\\\\\"
# -----------------在某一种字体下,对字库遍历,生成相应字体图片------------------------
for i in range(num_letter-1):
letterList[j].GenLetterImage(Library[i])
grayImg = ImageOps.grayscale(letterList[j].img)
grayImg.save(paths+str(i)+\".png\")
流程图:
上一篇 :
Windows 微秒级 延时
-
高危预警|RDP漏洞或引发大规模蠕虫爆发,用户可用阿里云免费检测服务自检,建议尽快修复
2026-05-18栏目: 教程
-
云上一指禅:大数据产品DataWorks每日问答
2026-05-18栏目: 教程
-
云数据库RDS是什么?
2026-05-18栏目: 教程
-
2019智能安防工程师大会在杭州成功召开
2026-05-18栏目: 教程
-
2018年中国专利申请154万件 超过日美 占全球近半
2026-05-18栏目: 教程
您的足迹:
