Discuss / Python / 交作业

交作业

Topic source

久疤_796

#1 Created at ... [Delete] [Delete and Lock User]

仿照@求实-探针写的

import os
import struct
from enum import Enum,unique
class mybmp(object):
    @unique
    class bmpindex(Enum):
        size = 2
        width = 6
        height = 7
        color = 9
    def __init__(self,file):
        with open(file,'rb') as f:
            text = f.read(30)
            info = struct.unpack('<ccIIIIIIHH',text)
            print(info)
            self.type = (info[0]+info[1]).decode().upper()
            self.info = info
    def __getattr__(self,name):
        try:
            return self.info[self.bmpindex[name].value]
        except:
            return None
    def check(self,hope='BM'):
        if self.type==hope.upper():
            return '%dx%d\r\n%d'%(self.width,self.height,self.color)
        else:
            return False

if __name__=='__main__':
    path = os.path.abspath('.')
    file = os.path.join(path,'test.BMP')

    info = mybmp(file)
    print(info.check('BM'))

  • 1

Reply