按字节读文件

在读取手写藏文字符数据集的时候,下载下来的文件是.pot文件,一开始以为是ppt的模块文件,后来发现ppt打不开这种文件,重读官网数据集说明,发现了下图:


13979953-8a1e8803f005d348.png
图片来自http://www.csdata.org/en/p/19/

可以看出来,这个pot文件是自定义的文件格式,里面的数据按2B存储,因此读数据也要每次两个字节这样来读

with open(filename,'rb',True) as f:
    content = f.read()

由图可知,每个pot文件中的每个藏文字符的坐标点结束后的坐标是(-1,-1),每个藏文字符的每个笔画结束后的坐标是(-1,0)。b'\xff\xff\xff\xff'正是-1,-1

allData = content[:]
characters = allData.split(b'\xff\xff\xff\xff')[:-1]

for character in characters:
    count += 1
    code = int.from_bytes(character[2:4],byteorder='little') #两个字节两个字节的读
    codes.append(code)
    pointBytes = character[6:]
    length = len(pointBytes)
    print(code)
    points = []
    for i in range(math.ceil(length/2)):
        points.append(int.from_bytes(pointBytes[2*i:2*i+2],byteorder='little'))
       #两个字节两个字节的读
    pl = np.array(points).reshape(-1,2)
    #一笔一笔地将藏文字符画出来
    plt.figure(count)
    ac = plt.gca()  
    op = []
    for p in pl:
        if(p[0]==65535):
            op = np.array(op)
            ac.set_xlim([op[:,0].min()-40,op[:,0].max()+40])
            ac.set_ylim([op[:,1].min()-40,op[:,1].max()+40])
            plt.plot(op[:,0],op[:,1],c='r',linewidth='1')
            op = []
        else:
            p[1] = -1*p[1]
            op.append(list(p))
    plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值