python暴力破解字体加密

from fontTools.ttLib import TTFont
import ddddocr
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont


def convert_cmap_to_image(cmap_code, font_path):
    img_size = 1024
    # 准备三要素:image画布  draw画笔 font字体
    img = Image.new("1", (img_size, img_size), 255)  # 创建一个黑白图像对象
    draw = ImageDraw.Draw(img)  # 创建绘图对象
    font = ImageFont.truetype(font_path, img_size)  # 加载字体文件

    # 将 cmap code 转换为字符
    character = chr(cmap_code)
    # print("character:",character)
    bbox = draw.textbbox((0, 0), character, font=font)  # 获取文本在图像中的边界框
    width = bbox[2] - bbox[0]  # 文本的宽度
    height = bbox[3] - bbox[1]  # 文本的高度
    draw.text(((img_size - width) // 2, (img_size - height) // 2), character, font=font)  # 绘制文本,并居中显示
    return img


def extract_text_from_font(font_path):
    font = TTFont(font_path)  # 加载字体文件
    font.saveXML("xxx.xml")
    # 图像识别的模块:DdddOcr
    ocr = ddddocr.DdddOcr(beta=True, show_ad=False)  # 实例化 ddddocr 对象

    print("font.getBestCmap().items():", font.getBestCmap().items())

    font_map = {}
    for cmap_code, glyph_name in font.getBestCmap().items():
        image = convert_cmap_to_image(cmap_code, font_path)  # 将字体字符转换为图像

        # 提取图像字符
        bytes_io = BytesIO()
        image.save(bytes_io, "PNG")
        text = ocr.classification(bytes_io.getvalue())  # 图像识别
        print("text:", text)
        # image.save(f"./imgs/{text}.png", "PNG")  # 保存图像

        print(f"Unicode码点:{cmap_code} - Unicode字符:{glyph_name},识别结果:{text}")
        font_map[cmap_code] = text

    return font_map


font_file_path = "font.woff"
print(extract_text_from_font(font_file_path))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值