Discuss / Python / 答案

答案

Topic source
def bmp_info(data):
    data = data[:30] #截取前面30个字节
    info = struct.unpack('<ccIIIIIIHH',data)
    if info[0] + info[1] == b'B' + b'M': #判断是否是Windows位图文件
        width = info[6]
        height = info[7]
        color = info[9]
        return {
            'width': width,
            'height': height,
            'color': color
        }
    else:
        raise TypeError('File is bad')

  • 1

Reply