Created
September 14, 2020 23:48
-
-
Save kylebarron/9f072effabe5d5047ebd2cfd47d5841a to your computer and use it in GitHub Desktop.
Pack 12-bit buffer (very inefficient right now)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zlib | |
from io import BytesIO | |
# pip install bitstruct | |
import bitstruct | |
import numpy as np | |
arr = np.random.randint(0, 4096, size=(256, 256), dtype=np.uint16) | |
buf = BytesIO() | |
np.save(buf, arr) | |
buf.tell() | |
buf.seek(0) | |
compressed_buf = zlib.compress(buf.getvalue()) | |
len(compressed_buf) | |
# 112635 compressed | |
# 131200 uncompressed | |
# 14% savings | |
flat = arr.flatten() | |
exp_byte_length = int(12 * 256 * 256 / 8) | |
data = bytearray(exp_byte_length) | |
# This is a horribly slow way to pack the data, but I didn't want to spend the | |
# time yet thinking through an efficient, vectorized approach. | |
offset = 0 | |
for val in flat: | |
bitstruct.pack_into('u12', data, offset, val) | |
offset += 12 | |
len(zlib.compress(data)) | |
# 98340 | |
# I think there's no improvement with compression because the original data was | |
# completely random? (Created with np.random.randint) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment