字体反爬之自动化通过字体文件生成映射字典

1、首先找到以.ttf结尾的字体文件,下载下来,以我的字体文件sfont.ttf为例

sont.ttf下载地址https://download.csdn.net/download/lingyingdon/89534953

目前只测试了.ttf文件。如果想使用woff字体文件,请自行测试

2、下载分割字体文件的软件fontforge, 安装后将安装路径添加到环境变量中去,该软件结合python脚本分割字体文件为单个字体图片及其对应的编码作为文件名

  • 官网地址如下

    https://fontforge.org/en-US/

  • python脚本如下split_font.py

    import os
    import argparse
    import sys
    
    import fontforge
    
    
    def main(font_path, folder):
        # 字体文件分割脚本,需要配合fontforge使用
        # font_path = r"F:\download\sfont.ttf"  # 字体文件路径
        # folder = "img"  # 字体文件分割后保存的目录
        F = fontforge.open(font_path)
        for name in F:
            filename = name + ".png"
            export_path = os.path.join(folder, filename)
            F[name].export(export_path)
            
    if __name__ == '__main__':
    
        parser = argparse.ArgumentParser(description='字体分割.....')
        parser.add_argument('-f', '--file_path', type=str, help='字体文件路径,字体文件为.ttf结尾')
        parser.add_argument('-d', '--dir', type=str, help='输出字体文件目录')
    
        args = parser.parse_args()
        if args.file_path and args.dir:
            main(args.file_path, args.dir)
        else:
            print("请输入字体文件路径和输出字体文件目录")
            sys.exit(1)
    
  • 通过执行以下命令脚本分割字体文件(前提是将fontforge添加到环境变量)

    fontforge split_font.py

  • 处理后的图片如下

    在这里插入图片描述

3、由于分割后的字体文件相对比较模糊,通过使用pillow模块扩张字体图片大小来增加图片的清晰度

def strength_pic(pic_path):
    """
    图片增强
    猜想是,在进行卷积处理的时候,选择的算子在边界处理上更倾向于重新计算,而实际上我们的边界是不需要计算的,所以这里手动将边界扩张
    """

    old_im = Image.open(pic_path)
    old_size = old_im.size

    new_size = (300, 300)
    new_im = Image.new("RGB", new_size, color='white')  ## luckily, this is already black!
    new_im.paste(old_im, (int((new_size[0] - old_size[0]) / 2),
                          int((new_size[1] - old_size[1]) / 2)))

    new_im.save(pic_path.replace('img', 'img_output'))
  • 经过处理后的图片如下

    在这里插入图片描述

4、使用ddddocr模块来识别字体图片

def ocr_img(ocr, file_path):
    """
    使用ddddocr模块识别
    :param ocr:  ddddocr实例化对象
    :param file_path:  图片文件路径
    :return:  识别结果
    """
    image = open(file_path, "rb").read()
    result = ocr.classification(image)
    return result

5、最后来看一下运行结果,全自动执行,不需要在一个一个整理字体字典了(我这里在代码中用&#x对uni进行了替换)

在这里插入图片描述

想要完整代码的联系fangyingdon@163.com

  • 16
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis Plus 是 MyBatis 的增强工具,在 MyBatis 的基础上增加了许多实用的功能,使得 MyBatis 的使用更加方便和高效。MyBatis Plus 提供了代码生成器,可以帮助我们快速生成 Mapper 接口和 XML 映射文件。 下面是 MyBatis Plus 生成映射文件的步骤: 1. 在 pom.xml 文件中添加 MyBatis Plus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>最新版本号</version> </dependency> ``` 2. 配置 MyBatis Plus 的代码生成器,在 application.yml 文件中添加以下配置: ```yml mybatis-plus: global-config: db-config: id-type: auto banner: false generator: # 数据源配置 datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root # 包配置 package-info: com.example.demo parent: com.example.demo moduleName: mybatis-plus # 策略配置 strategy: naming: underline_to_camel super-entity-class: com.baomidou.mybatisplus.extension.activerecord.Model entity-lombok-model: true super-controller-class: com.baomidou.mybatisplus.extension.api.ApiController include: t_user table-prefix: t_ ``` 其中,datasource 配置数据源信息,package-info 配置包信息,strategy 配置生成策略,可以根据实际情况进行修改。 3. 运行代码生成器,生成 Mapper 接口和 XML 映射文件。在项目中新建一个 Generator 类,添加以下代码: ```java public class Generator { public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("author"); gc.setOpen(false); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("mybatis-plus"); pc.setParent("com.example.demo"); mpg.setPackageInfo(pc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setInclude("t_user"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix("t_"); mpg.setStrategy(strategy); // 执行生成 mpg.execute(); } } ``` 运行 Generator 类即可生成 Mapper 接口和 XML 映射文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序烂人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值