2017-10-29 18 views
1

파이썬으로 bmp 파일의 헤더를 읽을 필요가 있습니다. 나는이 같은 시도했지만 분명히 비 이해할 바이트 만 무리를 반환파이썬에서 bmp 파일 헤더를 읽는 방법?

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

내 아이디어를 여는 동안 이미지 정보를 기록하기 위해, 모듈, 또는 고속 무언가를 찾는 것이 었습니다. 나는 정확하게 원하는 것을 수행하는 matlab에서이 함수에 대해 알고있다 : imfinfo(). 하지만 파이썬에서 대응 물을 찾을 수는 없습니다.

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

이 이미지 유형을 추출합니다 :

 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.Image.info – jmetz

+0

PIL이게'{ 'dpi': (0, 0), 'compression': 0}' – RobiNoob

+0

입니다. 당신이'struct' 모듈을 봐야한다고 생각합니다. https://docs.python.org/3/library/struct.html#module-struct –

답변

1

당신은 (파이썬 다음 stdlib에)에 imghdr module가 사용할 수 있습니다

명확하게하려면,이게 내가 MATLAB으로 무엇을 얻을 헤더에서,하지만 그게 전부입니다. 파이썬 표준 라이브러리에는 더 자세한 정보를 얻을 수있는 것이 없으므로 이러한 특수 작업을 수행하려면 타사 라이브러리가 필요합니다. 이것의 복잡성에 대한 아이디어를 얻으려면 BMP file format을보십시오. 여기에 설명 된 표준을 기반으로 몇 가지 정보 항목을 추출하기 위해 순수 Python 코드를 작성하는 것이 가능할 수 있지만 임의의 비트 맵 이미지 파일에 적합하게 만드는 것은 쉽지 않습니다.

UPDATE : 아래

struct module를 사용하여 비트 맵 헤더에서 몇 가지 기본 정보를 추출하는 간단한 스크립트입니다. 다양한 값을 해석하는 방법은 위에서 언급 한 BMP 파일 형식을 참조하고,이 스크립트는 형식의 가장 일반적인 버전 (예 : 윈도우 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