Discuss / Python / 好羞耻,又做了代码的搬运工

好羞耻,又做了代码的搬运工

Topic source
import struct


def bmp(path):
    with open(path, 'rb') as f:
        byte = f.read(30)  # 打开BMP位图读取前30个字节
        rb = struct.unpack('<ccIIIIIIHH', byte)  # unpack把bytes变成相应的数据类型
        if rb[0] == b'B' and rb[1] == b'M':
            return ('图片分辨率: %s x %s, \n颜色数: %s ' % (rb[6], rb[7], rb[-1]))
        else:
            raise TypeError('It\'s not bmp file! ')

path = '/home/uxeix/Pictures/bmp/robit.bmp'
path2 = '/home/uxeix/Pictures/Wallpapers/cold-wallpaper-3.jpg'
print(bmp(path))
print(bmp(path2))

  • 1

Reply