Discuss / Python / 练习

练习

Topic source

!/usr/bin/env python

Filename: bmpinfo.py

Tell if a file is a bitmap, if so, print its info.

import sys, struct from collections import namedtuple

if len(sys.argv) < 2: exit()

BitmapInfo = namedtuple('BitmapInfo', ['sign', 'size', 'save', 'offset', 'header_bytes', 'width', 'height', 'const', 'ncolor'])

with open(sys.argv[1], 'rb') as f: content = f.read(30) bmpinfo = BitmapInfo(*struct.unpack('<2sIIIIIIHH', content))

print(bmpinfo)


  • 1

Reply