generator.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:DeepLearning-OCR 作者: xingjian-f 项目源码 文件源码
def captcha_draw(label, fonts, dir_path, pic_id):
    # width, height = 512, 48
    # size_cha = random.randint(24, 48) # ????
    # derx = random.randint(0, 16)
    # im = Image.new(mode='L', size=(width, height), color='white') # color ?????size ????
    # drawer = ImageDraw.Draw(im)
    # font = ImageFont.truetype(random.choice(fonts), size_cha)
    # drawer.text(xy=(derx, 0), text=label, font=font, fill='black') #text ???font ????????
    # # im.show()
    # write2file(dir_path, label, im)

    width, height = 32, 32
    size_cha = random.randint(16, 28) # ????
    derx = random.randint(0, max(width-size_cha-10, 0))
    dery = random.randint(0, max(height-size_cha-10, 0))
    im = Image.new(mode='L', size=(width, height), color='white') # color ?????size ????
    drawer = ImageDraw.Draw(im)
    font = ImageFont.truetype(random.choice(fonts), size_cha)

    drawer.text(xy=(derx, dery), text=label, font=font, fill='black') #text ???font ????????
    # if label != ' ' and (img_as_float(im) == np.ones((48, 48))).all():
    #     # in case the label is not in this font, then the image will be all white
    #     return 0
    im = im.convert('RGBA')
    max_angle = 45 # to be tuned
    angle = random.randint(-max_angle, max_angle)
    im = im.rotate(angle, Image.BILINEAR, expand=0)
    fff = Image.new('RGBA', im.size, (255,)*4)
    im = Image.composite(im, fff, im)
    # if random.random() < 0.5:
    #     im = Image.fromarray(grey_erosion(im, size=(2, 2))) # erosion
    # if random.random() < 0.5:
    #     im = Image.fromarray((random_noise(img_as_float(im), mode='s&p')*255).astype(np.uint8))
    # im = im.filter(ImageFilter.GaussianBlur(radius=random.random()))
    # im.show()
    write2file(dir_path, label, im, pic_id)
    return 1
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号