10.4、

Python提供了一个struct模块来解决bytes和其他二进制数据类型的转换。

structpack函数把任意数据类型变成bytes

 >表示字节顺序是big-endian,也就是网络序,I表示4字节无符号整数。

 I:4字节无符号整数和H:2字节无符号整数

def bmp_info(data):
    bmp=struct.unpack('<ccIIIIIIHH',data[:30])  #获取前30个字节
    if bmp[0]==b'B' and bmp[1]==b'M':
        dict={}
        dict['width']=bmp[6]
        dict['height']=bmp[7]
        dict['color']=bmp[9]
        return dict

bi = bmp_info(bmp_data)
assert bi['width'] == 28
assert bi['height'] == 10
assert bi['color'] == 16
print('ok')

ok

猜你喜欢

转载自www.cnblogs.com/soberkkk/p/12656308.html