【问题标题】:How to read bmp file header in python?如何在python中读取bmp文件头?
【发布时间】: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

【问题讨论】:

标签: python image image-processing bmp file-header


【解决方案1】:

这是一个简短的示例,如何使用 struct module 提取整个 bmp 标头信息。

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)

如果您不想保存整个标头数据,而只想从中获取一个值,则可以使用 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)

【讨论】:

    【解决方案2】:

    您可以使用imghdr module(在 python 标准库中):

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

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

    更新

    下面是一个使用struct module 从位图标题中提取一些基本信息的简单脚本。请参阅上面提到的 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        
    

    【讨论】:

    • 这只是返回图像扩展名。我在找文件头
    • @RobiNoob。它不仅返回扩展名,还从标题中确定图像的类型。但无论如何,您实际上需要哪些具体信息?
    • @RobiNoob。是的,我知道 - 但您真正需要的具体信息是什么?如果你想要 matlab 给你的东西,那么就使用它。如果你想要更快的东西,你可能不得不满足于更少。
    • 我正在将一个脚本从 matlab 翻译成 python,所以我不能使用 matlab 啊。无论如何,我想检索仅加载图像时无法访问的信息。例如,我可以执行 img.shape 或 img.size 来获取有关像素的信息,但我不知道如何获取例如格式版本
    • @RobiNoob。位图标题可以包含相当多的信息,其中一些是特定于应用程序的。您能否给出您需要的具体物品的准确清单?如果你把它限制在你真正需要的范围内,用纯 python 编写一次性解决方案可能是可行的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2021-07-06
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多