读取.png的有效位数?

2024-06-11 14:07:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一套16位pngs文件,用LabVIEW录制。出于某种原因,我需要改变它们来使用它们。对于这个,我需要有效位的数量。如何使用Python阅读这些内容?在Matlab中,有一种方法叫做imfinfo,它返回有效位。你知道吗


Tags: 文件方法内容数量matlablabviewpngsimfinfo
1条回答
网友
1楼 · 发布于 2024-06-11 14:07:50

正如@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)

相关问题 更多 >