使用PaddleOCR自动合并防疫健康图片

# 合并防疫健康图
import shutil
import os
from paddleocr import PaddleOCR
from PIL import Image, ImageFont, ImageDraw
import time


# 列出当前目录下所有图片名称
path = os.getcwd()
files = os.listdir(path)
im_files = [im_file for im_file in os.listdir(path) if im_file.endswith(('jpg', 'png', 'gif', 'jpeg'))]

# 以中文模式识别
ocr = PaddleOCR(lang='ch')

# 绘图使用字体、大小
font = ImageFont.truetype('STZHONGS.TTF', size=80)

# 获取所有人员信息
names = []
with open('jkm_config.ini', 'r', encoding='utf-8') as f:
    for line in f.readlines():
        line = line.strip()
        if not line.startswith('#'):
            names.append((line.split()[0], line.split()[1]))


# 规范化命名图片
def OCR_rename(im_file):
    """
    规范化命名图片
    返回姓名
    """
    # 图片后缀
    suffix = im_file.split('.')[1]
    res = ocr.ocr(im_file, cls=True)
    res_str = ''
    for lin in res:
        res_str = res_str + lin[1][0] + '\n'

    new_file_name = ''
    pic_name = ''
    for name, tel in names:
        # print(name, tel)
        if ('健康码' in res_str) and (name in res_str):
            new_file_name = name + '健康码'
            pic_name = name
        elif ('核酸检测记录' in res_str) and (name in res_str):
            new_file_name = name + '核酸'
            pic_name = name
        elif ('通信大数据行程卡' in res_str) and (tel[:3] in res_str and tel[-4:] in res_str):
            new_file_name = name + '行程卡'
            pic_name = name

    if new_file_name:
        shutil.move(im_file, new_file_name + '.' + suffix)
        print(new_file_name, '已完成重命名')
        return pic_name


# 将图片拼接成一张图,并进行添加信息
def OCR_joint(name, tel):
    # 起始位置
    x = 0
    # 单张图宽度
    w = 500
    # 单张图高度
    h = 1000
    # 创建新图
    new_image = Image.new('RGB', (3 * w, h + 100), 'WHITE')
    draw = ImageDraw.Draw(new_image)

    im1 = Image.open(name + '健康码.jpg')
    im1 = im1.resize((w, h))        # 将图片重置大小
    new_image.paste(im1, (x, 100))  # 将图片粘贴至新图上
    x += w

    im2 = Image.open(name + '核酸.jpg')
    im2 = im2.resize((w, h))
    new_image.paste(im2, (x, 100))
    x += w

    im3 = Image.open(name + '行程卡.jpg')
    im3 = im3.resize((w, h))
    new_image.paste(im3, (x, 100))

    # 绘制标注
    draw.text(xy=(500, 0), text=name+'\t'+tel, fill=(255, 0, 0), font=font)
    # 生成三合一拼接图
    new_image.save(name + '防疫健康图.jpg')


def main():
    pic_names = set()
    for im_file in im_files:
        pic_name = OCR_rename(im_file)
        # print(pic_name)
        pic_names.add(pic_name)
    # print(pic_names)

    for name in names:
        try:
            # print(name[0])
            if name[0] in pic_names:
                OCR_joint(name[0], name[1])
        except Exception as e:
            print(e)

    print('完成')


if __name__ == '__main__':
    # time_start = time.time()
    main()
    # tim = time.time() - time_start
    # print(tim)

将姓名、手机号信息存放在config.ini配置文件中

#每行格式为姓名 手机号
张三  12323459876

合并效果如下:

posted @ 2022-07-25 21:05  areke  阅读(144)  评论(0)    收藏  举报