2016-11-04 21 views
0

我有一組16位pngs文件,用LabVIEW錄製。出於某種原因,我需要轉移他們來使用它們。爲此,我需要有效位的數量。我如何閱讀那些使用Python?在Matlab中,有一種方法稱爲imfinfo它返回有效位。閱讀.png有效位的數目?

+0

你可以在Python3的'int'與'.bit_length'方法的顯著位的數量。 –

+1

假定16位PNG每個顏色分量有16個有效位,但PNG也有一個可選的「sBIT」塊,如果存在的話,它傳達每個組件的有效位數。將這些信息與@ PM2Ring建議的.bit_length方法的值結合起來,以瞭解您需要多少位移。 –

回答

0

如建議通過@Glen蘭德斯·皮爾遜,我剛讀了SBIT塊:

import struct 
import binascii 

def __header(bytes): 
    return struct.unpack('>NNccccc', bytes) 

def __getSBit(bytes): 
    bytes = bytes[8:] 

    sBit = 0 

    while bytes: 
     length = struct.unpack('>I', bytes[:4])[0] 
     bytes = bytes[4:] 

     chunk_type = bytes[:4] 

     bytes = bytes[4:] 

     chunk_data = bytes[:length] 
     bytes = bytes[length:] 

     if chunk_type == "sBIT": 
      sBit = int(chunk_data.encode("hex"), 16) 
      break 

     bytes = bytes[4:] 

    return sBit 

def getSigniticantBits(filename): 
    with open(filename, 'rb') as f: 
     bytes = f.read() 

    return __getSBit(bytes)