Discuss / Python / answer

answer

Topic source

siusuu

#1 Created at ... [Delete] [Delete and Lock User]
import struct, os

def bmpinfo(pic):
    if not isinstance(pic, str):
        return 'Not BMP!'
    if os.path.splitext(pic)[1] != '.bmp':
        return 'Not BMP!'
    try:
        f = open(pic, 'rb')
        s = f.read(30)
        info = struct.unpack('<ccIIIIIIHH', s)
        if not info[0] == b'B' and info[1] == b'M':
            return: 'Not BMP!'
        print(r'size : %s Byte; %s * %s ;depth_of_color : %s' % (info[2], info[6], info[7], info[9]))
    except IOError as e:
        print(e)
        return 'wrong path!'
    finally:
        if f:
            f.close()

  • 1

Reply