读取.png的有效位数?

时间:2016-11-04 18:52:34

标签: python png

我有一套16位的pngs文件,用LabVIEW录制。出于某种原因,我需要将它们移位以使用它们。为此,我需要有效位数。如何阅读使用Python的内容?在Matlab中,有一个名为 imfinfo 的方法,它返回有效位。

1 个答案:

答案 0 :(得分:0)

根据@Glen Randers-Pehrson的建议,我刚读了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)