2017-10-29 17 views
1

私はPythonでbmpファイルのヘッダを読む必要があります。私はこのようにしようとしたが、それは明らかに非分かりバイトのちょうど束を返します。Pythonでbmpファイルヘッダを読むには?

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

私の考えは、それを開いている間に画像情報を記録するために、モジュール、または高速何かを見つけることでした。私は正確に何をしているのmatlabでこの機能について知っている:imfinfo()。しかし、私はPythonでそれを見つけることができません。

明確にするために、これは私はMathWorks社の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 
+0

'PIL'の' Image.info'を試しましたか? http://effbot.org/imagingbook/image.htm#tag-Image.Ifo – jmetz

+0

PILこれは私が '{'dpi':(0、0)、 'compression':0}'を得るものです。 – RobiNoob

+0

Iあなたは 'struct'モジュールを見るべきだと思います。 https://docs.python.org/3/library/struct.html#module-struct –

答えて

1

あなたは(PythonのSTDLIBである)imghdr moduleを使用することができます。

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

これは、画像の種類を抽出しますヘッダーから、それはすべてです。 Python標準ライブラリには、より詳細な情報を得ることのできるものは何もありません。そのような特別な作業を行うには、サードパーティのライブラリが必要です。この複雑さのアイデアを得るには、BMP file formatをご覧ください。そこに概説されている仕様に基づいて、いくつかの情報項目を抽出する純粋なPythonコードを書くことは実現可能かもしれませんが、任意のビットマップ画像ファイルに対してそれを正しく取得することは容易ではありません。

UPDATE:以下

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   
+0

これは画像の拡張子を返します。私はファイルのヘッダ – RobiNoob

+0

@ RoobNoobを探しています。拡張子を返すだけでなく、ヘッダーから画像のタイプを決定します。しかし、とにかく、具体的にどのような情報が実際に**必要です**。 – ekhumoro

+0

私はちょうどOP – RobiNoob