字体反爬对抗

界面上显示明明是这个,复制下来却是 “微北班略声讲哪取,随土便常师想纪撰还,提述融售母酒纪个?” 简单分析后发现这些文字都使用了特殊的反爬字体,其实就是经过了置换处理。

image.png

对于这种字体反爬场景可以,使用通过 OCR 识别拉表对抗,下面是通过 paddle 的 OCR 模型的一种实现。

import json
import os.path

from fontTools.ttLib import TTFont
from PIL import Image, ImageDraw, ImageFont
from paddleocr import PaddleOCR

def get_character_image(unicode_char, size=120, color=(0, 0, 0), background=(255, 255, 255)) -> Image:
# 使用 Pillow 创建图像
img = Image.new('RGB', (size, size), background)
draw = ImageDraw.Draw(img)

# 加载字体
font = ImageFont.truetype('exam_font_75f40cd3f2514aae89c62ff49d571f02.ttf', 100)

# 获取文本的宽度和高度
_, _, text_width, text_height = font.getbbox(unicode_char)

# 在图像中央绘制文本
x = (size - text_width) / 2
y = (size - text_height) / 2
draw.text((x, y), unicode_char, font=font, fill=color, stroke_width=1.0)

return img

def main():
exam_font = TTFont('./exam_font_75f40cd3f2514aae89c62ff49d571f02.ttf')
ocr = PaddleOCR(lang='ch')
rec_result = dict()
for table in exam_font['cmap'].tables:
if table.isUnicode():
for k in table.cmap.keys():
if chr(k) in rec_result:
continue
img_path = f'./images/{k}.png'
if not os.path.exists(img_path):
get_character_image(chr(k)).save(img_path)
text, _ = ocr.ocr(img_path, det=False, cls=False)[0][0]
print(f'{img_path}: {chr(k)} -> {text}')
rec_result[chr(k)] = text
with open('rec_result.json', 'w') as f:
rec_map_json = json.dumps(rec_result)
f.write(rec_map_json)

if __name__ == '__main__':
main()