Python绘制汉字点阵图(字符图)

目标:给定一段话,将这段话转换为字符的图片,比如说一句“哈哈哈”可以被转换为下面的字符矩阵。

999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999   99999999999999999999999999999   99999999999999999999999999999   99999999999
999999999999999999   99999999999999999999999999999   99999999999999999999999999999   99999999999
99        9999999      99999999999        9999999      99999999999        9999999      999999999
99   999  99999   999   9999999999   999  99999   999   9999999999   999  99999   999   99999999
99   999  9999   99999    99999999   999  9999   99999    99999999   999  9999   99999    999999
99   999                    999999   999                    999999   999                    9999
99   999     99999999999999  99999   999     99999999999999  99999   999     99999999999999  999
99   999  999999999999999999999999   999  999999999999999999999999   999  9999999999999999999999
99   999  999             99999999   999  999             99999999   999  999             999999
99        999    999999   99999999        999    999999   99999999        999    999999   999999
99    9   999   99999999  99999999    9   999   99999999  99999999    9   999   99999999  999999
9999999999999   99999999  9999999999999999999   99999999  9999999999999999999   99999999  999999
9999999999999             9999999999999999999             9999999999999999999             999999
9999999999999    999999   9999999999999999999    999999   9999999999999999999    999999   999999
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

核心思路:通过python的pygame库,生成文字图片,再逐像素转换为字符图。

安装依赖库

pip install pygame

查找一个中文字体文件,该字体文件将用于后续的字符图生成

locate *.ttf

使用下面的代码进行转换:

#encoding: utf-8
import numpy as np
import os
import pygame
import pygame.surfarray as surfarray
pygame.font.init()


def gen_pics(
    text, # The text you want to generate
    scale, # Larger scale value lead to smaller output figure
    font_path, # A system font path
    white_pixel = '_',
    black_pixel = '+',
    ):
    font = pygame.font.Font(font_path, 64)
    rtext = font.render(text, True, (0, 0, 0), (255, 255, 255))
    img = surfarray.pixels2d(rtext)
    scaleH = scale
    scaleW = max(1, scale // 2)
    result = ''
    for h in range(0,img.shape[1],scaleH):
        for w in range(0,img.shape[0],scaleW):
            grey = img[w:w+scaleH,h:h+scaleW].mean()
            if grey < 128:
                result += black_pixel
            else:
                result += white_pixel
        result += '\n'
    return result, img

if __name__ == '__main__':
    font_path = "/Library/Fonts/Arial Unicode.ttf"
    res, _ = gen_pics(
        text = "哈哈哈",
        scale = 4,
        font_path = font_path,
        white_pixel = ' ',
        black_pixel = '9',
        )
    print(res)

小应用:通过使用相近的字符生成字符图,将信息隐藏的加密方式<字符图加密>

《Python绘制汉字点阵图(字符图)》有一个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据