Skip to content

Instantly share code, notes, and snippets.

@jeremyfix
Created March 10, 2022 12:50
Show Gist options
  • Save jeremyfix/7ea8a8dd3bd28ad8c6a5447ca9fa2c07 to your computer and use it in GitHub Desktop.
Save jeremyfix/7ea8a8dd3bd28ad8c6a5447ca9fa2c07 to your computer and use it in GitHub Desktop.
Writting/reading a binary file in python with string, ints and numpy arrays
# coding: utf-8
# External imports
import numpy as np
FIELD_WIDTHS = {
'int': 16
}
ENDIAN = 'big'
def write_int(f, value):
assert(value < 2**FIELD_WIDTHS['int'])
encoded_value = value.to_bytes(FIELD_WIDTHS['int'],
byteorder=ENDIAN,
signed=False)
f.write(encoded_value)
def read_int(f):
decoded_value = int.from_bytes(f.read(FIELD_WIDTHS['int']),
byteorder=ENDIAN,
signed=False)
return decoded_value
def write_binary(filename):
print(">>>>>>>> Saving the binary file")
with open(filename, 'wb') as f:
# Write a text of variable size
# which should be longer than an 2^STR_LEN_SIZE
txt = 'This is a text of variable size'
print(f"Saving the string '{txt}'")
encoded = txt.encode('utf-8')
write_int(f, len(encoded))
f.write(encoded)
# Write a sequence of variable size image
sizes = [(124, 324), (394, 39), (39, 35)]
# Write the number of images
write_int(f, len(sizes))
for s in sizes:
img = (255 * np.random.random(s)).astype(np.uint8)
write_int(f, s[0])
write_int(f, s[1])
# Write the size of the image
f.write(img.tobytes())
print(f"Saved array with value at (10, 10) = {img[10, 10]}")
def read_binary(filename):
print(">>>>>>>> Reading the binary file")
with open(filename, 'rb') as f:
# Read the string size
txt_len = read_int(f)
# Read the string
txt = f.read(txt_len).decode('utf-8')
print(f"Loaded the string '{txt}'")
num_images = read_int(f)
print(f"{num_images} images to decode")
for i in range(num_images):
img_size = (read_int(f), read_int(f))
data = f.read(np.dtype(np.uint8).itemsize * img_size[0] * img_size[1])
img = np.frombuffer(data, dtype=np.uint8).reshape(img_size)
print(f"Read array with value at (10, 10) = {img[10, 10]}")
if __name__ == '__main__':
filename = 'replay.bofl'
write_binary(filename)
read_binary(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment