Discuss / Python / 作业

作业

Topic source
def bmp_info(dir):
    try:
        with open(dir,'rb') as f:
            s=f.read(30)
            return struct.unpack('<ccIIIIIIHH', s)
    except BaseException:
        print (dir+'不是bmp图片')

沙志刚

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

这个有点不严谨,因为拿个jpg也会被认为是可以的,稍改了下: import struct def bmpinfo(file): try: with open(file,'rb') as f: s = f.read(30) info = struct.unpack('<ccIIIIIIHH',s) if info[0] == b'B' and info[1] == b'M': return info[2],info[6],info[7] else: raise BaseException except BaseException: print(file+' is not a bmp file')


  • 1

Reply