Search code examples
pythonimage-processingpngzlibdeflate

How to decompress IDAT compressed data


i wrote some python code to extract each chunk of a PNG image , i've tried with 3 different images and it works perfectly , it also matches perfectly what's displayed in https://rameshvarun.github.io/binary-inspector/png/ when i open the corresponding file , my script stores each chunk in a dict with the size of the data of each chunck (converted into an int) the type of the chunk (name , ie IDAT for an IDAT chunk) the data (everything excluding the CRC and the size and the type) and the CRC as keys

i tried using the deflate method from the zlib library using zlib.decompress on the data key of each data key of each chunk , also tried adding the CRC but i keep getting the error zlib.error: Error -5 while decompressing data: incomplete or truncated stream


Solution

  • The data from the IDAT chunks need to be concatenated into a single binary stream for decompression. Or at least decompressed as if it were concatenated, using zlib.decompressobj. That data is a single deflate stream, regardless of how it was split into IDAT chunks.