#-*-coding:utf-8-*- import struct class bmpinfo(dict): def __init__(self, filepath): with open(filepath, 'rb') as f: bmp_type = f.read(2) bmp_type = ''.join(map(bytes.decode, struct.unpack('<cc', bmp_type))) if bmp_type not in ('BM', 'BA'): raise FormatError('%s is not bmp format' %filepath) if bmp_type == 'BM': self['windows_bmp'] = True else: self['mac_bmp'] = True bype_contents = f.read(28) self._initial_attributes(bype_contents) def _initial_attributes(self, byte_contents): infos = struct.unpack('<IIIIIIHH', byte_contents) self['size'] = infos[0] self['width'] = infos[4] self['height'] = infos[5] self['depth'] = infos[7] def __getattr__(self, name): if name not in self: return None return self[name] info = bmpinfo('test.bmp') print('size:%s, dept:%s' %(info.size, info.depth))
厉害,学习中。。。。
Sign in to make a reply
求实-探针