【问题标题】:How to create images with English alphabets (A-Z, a-z, 0-9) in Python?如何在 Python 中使用英文字母(A-Z、a-z、0-9)创建图像?
【发布时间】:2021-08-27 05:03:03
【问题描述】:

我想准备一个用于识别英文字母数字字符的数据集。我想训练我的深度学习模型来识别字符。图像大小应为 32 * 32 或 28 * 28。字符应随图像的高度和宽度缩放。下面给出的是预期的输出。

Character "A" scaled with the Image

我尝试过 getsize(),但我无法使用图像大小缩放字符。

from PIL import Image, ImageDraw, ImageFont
import os,glob
import numpy as np
import random

parentPath=r"CaptchaDataset\\"


charactersList=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9']
fontTypes=[]

for font in glob.glob("fonts\\*"): # Create Image for every font
    fontTypes.append(os.path.abspath(font))
    
for index,character in enumerate(charactersList):
    
    path=os.path.join(parentPath,character+"_"+str(index))
    os.mkdir(path)
    
    
    for imageCounter in range(len(fontTypes)):
        for repeats in range(3): #Number of Images
            img = Image.new('1', (28, 28), color = 'black')
            fnt = ImageFont.truetype(fontTypes[imageCounter], random.randint(30,50))
            w,h=fnt.getsize(character)
            d = ImageDraw.Draw(img)
            d.text(((28-w)/2-5,((28-h)/2)-5), character, font=fnt, fill=(255),align="center") #TO ALIGN CHARACTER IN CENTER
            img=np.pad(img,pad_width=10, mode='constant', constant_values=0) #Manually added padding
            img=Image.fromarray(img)
            img.save(path+"\\"+str(imageCounter)+"_"+str(repeats)+".jpg")

UnExpected Output of Above program. "g" is cropped

【问题讨论】:

  • 我会把角色画在一张大图上,然后裁剪成你想要的尺寸。

标签: python image image-processing deep-learning dataset


【解决方案1】:

解决方法很简单:先创建好字体再创建图片。

不是创建 28x28 像素的图像,而是绘制一个可能比图像大的字符,而是创建字体,获取大小,然后根据字体的大小创建图像。

  • 创建字体,并获取字体大小:

     fnt = ImageFont.truetype(fontTypes[imageCounter], random.randint(30, 50))
     w, h = fnt.getsize(character)
    
  • 根据字体大小设置图片大小并创建图片:

     img_w, img_h = w + 20, h + 20  # Add 20 pixels padding (assume 10 pixels from each side).
     img = Image.new('L', (img_w, img_h), color='black')  # Replace '1' with 'L' (8-bit pixels, black and white - we fill 255 so we can't use 1 bit per pixel)
    
  • 在图像中心绘制人物:

     d.text(((img_w-w)/2, (img_h-h)/2), character, font=fnt, fill=255, align="center") # TO ALIGN CHARACTER IN CENTER
    

这是一个完整的(更新的)代码:

from PIL import Image, ImageDraw, ImageFont
import os,glob
import numpy as np
import random

parentPath = "CaptchaDataset\\"

charactersList=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9']
fontTypes=[]

for font in glob.glob("fonts\\*"): # Create Image for every font
    fontTypes.append(os.path.abspath(font))
    
for index,character in enumerate(charactersList):    
    path = os.path.join(parentPath,character + "_" + str(index))

    if not os.path.exists(path):
        os.mkdir(path)
     
    for imageCounter in range(len(fontTypes)):
        for repeats in range(3): # Number of Images
            #img = Image.new('1', (28, 28), color = 'black')
            fnt = ImageFont.truetype(fontTypes[imageCounter], random.randint(30, 50))
            w, h = fnt.getsize(character)
            img_w, img_h = w + 20, h + 20  # Add 20 pixels padding (assume 10 pixels from each side).
            img = Image.new('L', (img_w, img_h), color='black')  # Replace '1' with 'L' (8-bit pixels, black and white - we fill 255 so we can't use 1 bit per pixel)
            d = ImageDraw.Draw(img)
            d.text(((img_w-w)/2, (img_h-h)/2), character, font=fnt, fill=255, align="center") # TO ALIGN CHARACTER IN CENTER
            #img = np.pad(img, pad_width=10, mode='constant', constant_values=0) #Manually added padding
            #img = Image.fromarray(img)
            img.save(path + "\\" + str(imageCounter) + "_" + str(repeats) + ".jpg")

样本结果很少:

【讨论】:

    猜你喜欢
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多