【字体加密解决方案】某茄小说、某眼电影、某车帝、二手车网站

字体加密解码小工具

假设我们遇到了字体加密下载他的字体,转一个ttf

先把woff2转xml方便取出cmap ,然后吧所有cmap的key拿出来

python 复制代码
from  fontTools.ttLib import TTFont

from font_parse.font_to_img import font_to_img

woff2_path = "y8dl0-28arr.ttf"
font = TTFont('e26e946d8b2ccb7.woff2')
font.saveXML('font1.xml')
font_map = font.getBestCmap()
print(font_map)
font.close()
unicode_list=[chr(key) for key, _ in font_map.items()]
print(unicode_list)
normal_dict, error_dict = font_to_img(unicode_list, woff2_path)
print(normal_dict)
print(error_dict)

这个key就是字对应的Unicode的码

我们拿到这个码先encode在decode保证编码 统一

先加载字体文件

然后开一个画布,创建绘画对象,配置好大小,在根据这个码和对应的字体文件,在图像中绘制文本

最后使用ocr识别这个图片是什么文字,这个方法有个95%成功率。也算是个懒人解决方案了

python 复制代码
from PIL import ImageFont, Image, ImageDraw

from cnocr import CnOcr
import numpy as np
def font_to_img(code_list, filename, ignore_flag=True, score=0.95):
    normal_dict = {}
    be_padding_dict = {}
    ocr = CnOcr()
    """
        将字体画成图片
        code_list: 加密字符列表
        filename: 字体文件
        ignore_flag:是否忽视 sorce 返回结果
        score: 识别准确率默认 95%以上
    """
    img_size = 1024
    font = ImageFont.truetype(filename, int(img_size * 0.7))  # 加载指定文件名的 TrueType 字体文件,并设置字体大小为图像高度的 70%
    for char in code_list:
        char_code = char.encode().decode()  # 编码成字节序列,然后再解码成字符串。确保字符编码的一致性。
        img = Image.new('1', (img_size, img_size), 255) # 创建 1024*1024 背景颜色为白色(255)。参数 '1' 表示使用二值化(单色)模式。
        draw = ImageDraw.Draw(img)  # 创建一个图像绘制对象,用于在图像上绘制文本
        x, y = draw.textsize(char_code, font=font)   # 计算绘制字符所需的文本区域大小。


        draw.text(((img_size - x) // 2, (img_size - y) // 2), char_code, font=font, fill=0)  # 在图像中心绘制文本,填充颜色为黑色(0)。这里通过计算使文本位于图像中心。

        # ocr部分
        # 将单通道 转为 三通道
        img = img.convert("RGB")
        # word = ocr.ocr_for_single_line("%s.jpg" % mame_ocr)
        word = ocr.ocr_for_single_line(np.array(img))
        if word["score"] >= score:
            # 处理重复名字
            # img.save("%s_%s.jpg" % (char_code, word["text"]))
            normal_dict[char_code] = word["text"]
        else:
            be_padding_dict[char_code] = word
            img.save("./image/%s_%s_be_padding.jpg" % (char_code, word["text"]))
            if ignore_flag:
                normal_dict[char_code] = word["text"]
    return normal_dict, be_padding_dict
相关推荐
岁月宁静7 小时前
五、《从零手撸 Agent》 — 什么是 Agent:从“会聊天”到“会做事”,差的就是一个循环
python·agent
梯度下降者7 小时前
Python - 函数
python·高阶函数·可调用对象·functools·嵌套作用域
starzy199010 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
wuyk55510 小时前
Spider-第16讲-线程池与竞态问题
大数据·爬虫·python·elasticsearch·搜索引擎
kyle~16 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享17 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者18 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.18 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人19 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__19 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python