名字

没想好

生成中文字图片

| Comments

最近在做一个中文ocr,感觉是个不可完成任务。不管怎么样还是得试试。 首先要做的就是获得训练数据。中文的数据不比英文,量大而且没有地方下载,唯一能做的就是自己生成,在网上找了写函数自己改改就成了下面的脚本了

首先是一个中文字符集编码的生成函数:


    def generate_ch():

        #Function which generate all chinese gb2312 characters 
        
        body = random.randint(0xA, 0xA)
        tail = random.randint(0, 0xF)
        chars = []
        
        heads = range(0xB0, 0xF8)
        bodys = range(0xA, 0xF+1)
        tails = range(0, 0xF+1)
        for head in heads:
            for body in bodys:
                for tail in tails:
                    val = ( head << 0x8 ) | (body << 0x4 ) | tail
                    #print "%x"%head,"%x"%body,"%x"%tail,"%x"%val
                    str = "%x" % val
                    if str.endswith("ff") or str.endswith("a0"):
                        continue
                    #special cases no characters with these codecs
                    if str == "d7fa" or str == "d7fb" or str == "d7fc" or str == "d7fd" or str == "d7fe" or str == "d7ff" :
                        continue
                    chars.append(str.decode('hex').decode('gb2312'))
        return chars

如果要生成中文字符图片,有很多办法,这里我用pygame打印每个字符

    # -*- coding: utf-8 -*-
    import pygame
     pygame.init()
    font = pygame.font.Font(os.path.join("fonts", "simfang.ttf"), 64)
    rtext = font.render("".join("b1a1".decode('hex').decode('gb2312')), True, (0, 0, 0), (255, 255, 255))
    pygame.image.save(rtext,"t.jpg" )

然后中文字符图片就出来了

要打印字符,需要至少一个ttf,也就是字体的文件。这里是用的simfang.ttf

Comments