如何在python中读取bmp文件头? [英] How to read bmp file header in python?

查看:584
本文介绍了如何在python中读取bmp文件头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用python读取bmp文件的标题。我试过这样但显然只返回一堆不可理解的字节:

I need to read the header of a bmp file with python. I tried like this but it obviously returns just a bunch of non intelligible bytes:

f = open(input_filename,"rb")
data = bytearray(f.read())
f.close()
print(data[:14])

我的想法是找到一个模块,或者快速的东西,以便在打开时记录图像信息。我在matlab中知道这个函数,它完全符合我的要求: imfinfo()。但是我找不到python中的对应物。

My idea was to find a module, or something fast, in order to log image info while opening it. I know about this function in matlab that does exactly what i want: imfinfo(). But I can't find a counterpart in python.

要清楚,这是我用matlab得到的:

To be clear, this is what I get with 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模块(位于python stdlib中):

You can use the imghdr module (which is in the python stdlib):

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

这将从中提取图像类型标题,但这就是全部。 Python标准库中没有任何其他内容可以获取更详细的信息 - 您需要第三方库来执行此类专门任务。要了解其复杂性,请查看 BMP文件格式。基于那里概述的规范,编写一些纯Python代码来提取一些信息可能是可行的,但是对于任意位图图像文件来说它是不容易的。

This will extract the image type from the header, but that is all. There is nothing else in the Python standard library that can get more detailed information - you need a third-party library to do such a specialized task. To get an idea of the complexity of this, take at look at BMP file format. Based on the specification outlined there, it might be feasible to write some pure Python code to extract a few items of information, but it won't be easy to get it right for an arbitrary bitmap image file.

UPDATE

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

Below is a simple script to extract some basic information from a bitmap header using the struct module. See the BMP file format mentioned above for how to interpret the various values, and note that this script will only work with the most common version of the format (i.e. 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        

这篇关于如何在python中读取bmp文件头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆