python生成带背景的字符图片(crnn数据集制作)

代码背景:

需求:识别汽车行驶证证芯号
图片示例:
在这里插入图片描述

解决方法:检测+识别(dbnet+crnn)
数据来源:真实图片+生成模拟图片
真实证芯号:
在这里插入图片描述
代码生成证芯号:


前期准备:

生成字符图片的背景图片

在这里插入图片描述

代码

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import re
from th import *

# 背景图片路径
background = 'background'
imagepath = os.listdir(background)
# 生成字符字体
font_list = ['msyh.ttc', 'Dengb.ttf', 'msyh.ttc', 'arial.ttf']

for i in range(0, 100):
    font = ImageFont.truetype(random.choice(font_list), 21)
    photo = Image.open(background + os.sep + (random.choice(imagepath)))
    w, h = photo.size
    add_number = ImageDraw.Draw(photo)  # 创建一个操作的对象
    # 设置需生成的字符
    text = random.sample('0123456789X0123456789', 13)
    text = ''.join(text)
    text = re.sub(r"(?<=\w)(?=(?:\w)+$)", "  ", text)
    add_number.text((int(w / 15), int(h / 2) - 10), u'*', font=ImageFont.truetype("C:\\Windows\\Fonts\\simsun.ttc", 21),
                    fill='black')
    add_number.text((int(w / 15) + 23, int(h / 2) - 12), u'%s' % ''.join(text), font=font, fill='black')
    add_number.text((int(w / 15) + 16 * 21 - 2, int(h / 2) - 10), u'*',
                    font=ImageFont.truetype("C:\\Windows\\Fonts\\simsun.ttc", 21), fill='black')
    # 图片变换处理
    photo = np.asarray(photo, order='F')
    photo = cv2.cvtColor(photo, cv2.COLOR_RGB2BGR)
    # photo=random_op(photo, op=random.randint(1, 3))
    cv2.imwrite(r"C:\Users\soli\Desktop\1/%s.jpg" % i, photo)

输出结果:

提示:这里填写问题的分析:

在这里插入图片描述


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

soli-lu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值