python图文识别案例

使用背景:

        破解font_family加密文件

import numpy
import pytesseract
from PIL import Image, ImageDraw, ImageFont
from fontTools.ttLib import TTFont


def font_convert(file_path):
    """
    采用ocr技术解析字体文件

    :param file_path: 字体文件存放地址
    :return: 字体解码的字典
    """
    font = TTFont(file_path)
    code_list = font.getGlyphOrder()[2:]
    im = Image.new("RGB", (1800, 1000), (255, 255, 255))
    dr = ImageDraw.Draw(im)
    font = ImageFont.truetype(file_path, 40)
    count = 15
    array_list = numpy.array_split(code_list, count)
    for t in range(count):
        new_list = [i.replace("uni", "\\u") for i in array_list[t]]
        text = "".join(new_list)
        text = text.encode('utf-8').decode('unicode_escape')
        dr.text((0, 50 * t), text, font=font, fill="#000000")
    result = pytesseract.image_to_string(im, lang="eng",
                                         config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789')
    result = result.replace(" ", "").replace("\n", "")
    code_list = [i.replace("uni", "&#x").lower() + ";" for i in code_list]
    return dict(zip(code_list, list(result)))


def parse_font_family(font_family: dict, origin_string):
    """
    字体解析工具

    :param font_family: 字体解码的字典
    :param origin_string: 需要解析的数据
    :return: 完成解析的数据
    """
    if not isinstance(origin_string, str):
        return origin_string
    for font in font_family:
        origin_string = origin_string.replace(font, font_family[font])
    return origin_string

 

posted @ 2021-07-07 21:38  羽墨1110  阅读(107)  评论(0编辑  收藏  举报