如何读取 python 中的 bmp 文件 header?

How to read bmp file header in python?

我需要用 python 读取 bmp 文件的 header。我这样试过,但显然 returns 只是一堆无法理解的字节:

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

您可以使用 imghdr module(在 python stdlib 中):

>>> import imghdr
>>> print(imghdr.what(input_filename))
bmp

这将从 header 中提取图像类型,仅此而已。 Python 标准库中没有其他任何东西可以获得更详细的信息 - 您需要一个 third-party 库来完成这样的专门任务。要了解其复杂性,请查看 BMP file format。根据那里概述的规范,编写一些纯 Python 代码来提取几项信息可能是可行的,但对于任意位图图像文件来说,要做到正确并不容易。

更新:

下面是一个使用 struct module 从位图 header 中提取一些基本信息的简单脚本。请参阅上面提到的 BMP 文件格式以了解如何解释各种值,并注意此脚本仅适用于该格式的最常见版本(即 Windows BITMAPINFOHEADER):

import struct

bmp = open(fn, 'rb')
print('Type:', bmp.read(2).decode())
print('Size: %s' % struct.unpack('I', bmp.read(4)))
print('Reserved 1: %s' % struct.unpack('H', bmp.read(2)))
print('Reserved 2: %s' % struct.unpack('H', bmp.read(2)))
print('Offset: %s' % struct.unpack('I', bmp.read(4)))

print('DIB Header Size: %s' % struct.unpack('I', bmp.read(4)))
print('Width: %s' % struct.unpack('I', bmp.read(4)))
print('Height: %s' % struct.unpack('I', bmp.read(4)))
print('Colour Planes: %s' % struct.unpack('H', bmp.read(2)))
print('Bits per Pixel: %s' % struct.unpack('H', bmp.read(2)))
print('Compression Method: %s' % struct.unpack('I', bmp.read(4)))
print('Raw Image Size: %s' % struct.unpack('I', bmp.read(4)))
print('Horizontal Resolution: %s' % struct.unpack('I', bmp.read(4)))
print('Vertical Resolution: %s' % struct.unpack('I', bmp.read(4)))
print('Number of Colours: %s' % struct.unpack('I', bmp.read(4)))
print('Important Colours: %s' % struct.unpack('I', bmp.read(4)))

输出:

Type: BM
Size: 287518
Reserved 1: 0
Reserved 2: 0
Offset: 1078
DIB Header Size: 40
Width: 657
Height: 434
Colour Planes: 1
Bits per Pixel: 8
Compression Method: 0
Raw Image Size: 286440
Horizontal Resolution: 11811
Vertical Resolution: 11811
Number of Colours: 256
Important Colours: 0        

这里是一个较短的示例,如何使用 struct module.

提取整个 bmp header 信息
import struct

with open(file_name, "rb") as f:
     file_data = f.read()

header_data = struct.unpack('<2sIHHIIIIHHIIIIII', file_data[:54])

print(header_data)

输出:

(b'BM', 690, 0, 0, 150, 124, 15, 9, 1, 32, 3, 0, 3780, 3780, 0, 0)

如果您不想保存整个 header 数据,而只想从中获取一个值,您可以使用 unpack_from()函数,可用于获取特定偏移量的数据。

这是一个例子:

import struct

with open(file_name, "rb") as f:
     file_data = f.read()

image_width = struct.unpack_from('<i', file_data, 18)[0]
image_height = struct.unpack_from('<i', file_data, 22)[0]

print(image_width, image_height)