0

I'm having trouble writing binary data out to a file.

My end goal is to add a chunk to a PNG file; which has a format of: length (4 bytes), type (4 bytes), data (lots of bytes), crc32 (4 bytes). Collecting each of these pieces is not a problem, but when the chunk is inserted, each item gets a "\n" (0x0a) appended to it, and the numeric items (length, crc32) get written as strings instead of just their numeric values.

f = open('output.png', 'wb')
f.write(prependBytes)
f.write(fileLength)
f.write(pngChunk)
f.write(fileData)
f.write(fileCRC32)
f.write(appendBytes)
f.close()

How do I get the numeric values written to file as just their number, and how do I get Python to not append a \n to each item it writes?

4
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by woooee
0

You should provide more information and example data. If you are working in python 3, a way to write a raw integer is f.write(bytearray((n,)). Otherwise what are the types of the values prependByte, fileLength, pngChunk, etc ?

0

but when the chunk is inserted, each item gets a "\n"

Python does not write a "\n" to a file as it does with a print statement. If there is a "\n" it must be in the string, so print the string so you know what is being added. Also, ImageMagick will append two PNG files so use it if you can as it is tested.

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.