最近参加了一个OCR手写数据集识别的比赛,为了对训练数据进行扩充(包括日期和金额两部分),记录一下生成手写图像的方法。
导入库
import time
from random import choice, randint, randrange
from PIL import Image, ImageDraw, ImageFont
字符集
characters = '拾伍佰正仟万捌贰整陆玖圆叁零角分肆柒亿壹元'
选取文字函数
def selectedCharacters(length):
result = ''.join(choice(characters) for _ in range(length))
return result
随机颜色产生函数
def getColor():
r = randint(0, 100)
g = randint(0, 100)
b = randint(0, 100)
return (r, g, b)
主函数
def main(size=(200, 100), characterNumber=6, bgcolor=(255, 255, 255)):
imageTemp = Image.new('RGB', size, bgcolor)
draw01 = ImageDraw.Draw(imageTemp)
text = selectedCharacters(characterNumber)
print(text)
font = ImageFont.truetype(font_path, 40)
width, height = draw01.textsize(text, font)
if width + 2 * characterNumber > size[0] or height > size[1]:
print('尺寸超出')
return
startX = 0
widthEachCharater = width // characterNumber
for i in range(characterNumber):
startX += widthEachCharater + 1
position = (startX, (size[1] - height) // 2)
draw01.text(xy=position, text=text[i], font=font, fill=getColor())
imageFinal = Image.new('RGB', size, bgcolor)
pixelsFinal = imageFinal.load()
pixelsTemp = imageTemp.load()
for y in range(size[1]):
offset = randint(-1, 0)
for x in range(size[0]):
newx = x + offset
if newx >= size[0]:
newx = size[0] - 1
elif newx < 0:
newx = 0
pixelsFinal[newx, y] = pixelsTemp[x, y]
draw02 = ImageDraw.Draw(imageFinal)
for i in range(int(size[0] * size[1] * 0.07)):
draw02.point((randrange(0, size[0]), randrange(0, size[1])), fill=getColor())
imageFinal.save("dataset/images/%d_%s.jpg" % (round(time.time() * 1000), text))
生成
if __name__ == '__main__':
font_path = "font/鸿雷板书简体-Regular.ttf"
for _ in range(2):
main((250, 48), 4, (209, 219, 189))
效果
|