def bmp_info(data):
if type(data) == str:
# str→bytes:encode()方法。str通过encode()方法可以转换为bytes。
# bytes→str:decode()方法。如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法。
data = str(data).encode()
if len(data) < 30:
return None
# 解析的字节数要等于unpack表示的
# c=1 I=4 H=2
# cc:2
# IIIIII: 24
# HH: 4
struct_list = struct.unpack('<ccIIIIIIHH', data[:30])
if (struct_list[0] == b'B' and struct_list[1] == b'M'):
return {
'width': int(struct_list[6]),
'height': int(struct_list[7]),
'color': int(struct_list[-1])
}
Sign in to make a reply
B O O M!
def bmp_info(data):
if type(data) == str:
# str→bytes:encode()方法。str通过encode()方法可以转换为bytes。
# bytes→str:decode()方法。如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法。
data = str(data).encode()
if len(data) < 30:
return None
# 解析的字节数要等于unpack表示的
# c=1 I=4 H=2
# cc:2
# IIIIII: 24
# HH: 4
struct_list = struct.unpack('<ccIIIIIIHH', data[:30])
if (struct_list[0] == b'B' and struct_list[1] == b'M'):
return {
'width': int(struct_list[6]),
'height': int(struct_list[7]),
'color': int(struct_list[-1])
}
return None