【发布时间】:2018-04-10 18:12:36
【问题描述】:
我需要用 python 读取一个 bmp 文件的头部。我试过这样,但它显然只返回一堆不可理解的字节:
f = open(input_filename,"rb")
data = bytearray(f.read())
f.close()
print(data[:14])
我的想法是找到一个模块,或者快速的东西,以便在打开它时记录图像信息。我知道 matlab 中的这个函数正是我想要的:imfinfo()。但是我在python中找不到对应的。
要清楚,这是我用 matlab 得到的:
FileModDate: '20-Oct-2017 09:42:24'
FileSize: 1311798
Format: 'bmp'
FormatVersion: 'Version 3 (Microsoft Windows 3.x)'
Width: 1280
Height: 1024
BitDepth: 8
ColorType: 'indexed'
FormatSignature: 'BM'
NumColormapEntries: 256
Colormap: [256x3 double]
RedMask: []
GreenMask: []
BlueMask: []
ImageDataOffset: 1078
BitmapHeaderSize: 40
NumPlanes: 1
CompressionType: 'none'
BitmapSize: 1310720
HorzResolution: 0
VertResolution: 0
NumColorsUsed: 256
NumImportantColors: 0
【问题讨论】:
-
你试过
PIL的Image.info吗? effbot.org/imagingbook/image.htm#tag-Image.Image.info -
用 PIL 这就是我得到的
{'dpi': (0, 0), 'compression': 0} -
我认为你应该看看
struct模块。 docs.python.org/3/library/struct.html#module-struct
标签: python image image-processing bmp file-header