0

我想自动创建不同字体的符号(数字、字母)图像数据集。我目前一直在通过在 Word 文档中截屏来手动执行此操作。有没有办法以编程方式执行此操作(最好在 Python 或 MATLAB 中)?

我知道这是一个简短的问题,但我在 SO 或网络上也找不到任何有用的起点。

4

1 回答 1

0

这是基于教程的示例:

import matplotlib
from PIL import ImageFont, ImageDraw, Image
import numpy as np
import random

#get a list of fonts on the system
system_fonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')

def create_image(text):
  #create blank image object
  size = random.randint(10,16)
  length = random.randint(4,8)
  img = np.zeros(((size*2)+5, length*size, 3), np.uint8)
  img_pil = Image.fromarray(img+255)

  # Drawing text in random font and random color
  font = ImageFont.truetype(random.choice(system_fonts), size)
  draw = ImageDraw.Draw(img_pil)
  draw.text((5, 10), text, font=font, fill=(random.randint(0,255), random.randint(0,255), random.randint(0,255)))

  #save image
  img_pil.save(f"{text}.jpg")

您可以解析单个文本 ( create_image('test')) 或生成随机字符串列表并循环它们。

于 2021-07-27T22:00:34.257 回答