Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

binary - Python: Fast way to read/unpack 12 bit little endian packed data

How can I speed up reading 12 bit little endian packed data in Python?

The following code is based on https://stackoverflow.com/a/37798391/11687201, works but it takes far too long.

import bitstring
import numpy as np

# byte_string read from file contains 12 bit little endian packed image data
# b'xABxCDxEF' -> pixel 1 = 0x0DAB, pixel 2 = Ox0EFC
# width, height equals image with height read
image = np.empty(width*height, np.uint16)

ic = 0
ii = np.empty(width*height, np.uint16)
for oo in range(0,len(byte_string)-2,3):    
    aa = bitstring.BitString(byte_string[oo:oo+3])
    aa.byteswap()
    ii[ic+1], ii[ic] = aa.unpack('uint:12,uint:12')
    ic=ic+2
question from:https://stackoverflow.com/questions/65851056/python-fast-way-to-read-unpack-12-bit-little-endian-packed-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work a bit better:

for oo in range(0,len(byte_string)-2,3):
    (word,) = struct.unpack('<L', byte_string[oo:oo+3] + b'x00')
    ii[ic+1], ii[ic] = (word >> 12) & 0xfff, word & 0xfff
    ic += 2

It's very similar, but instead of using bitstring which is quite slow, it uses a single call to struct.unpack to extract 24 bits at a time (padding with zeroes so that it can be read as a long) and then does some bit masking to extract the two different 12-bit parts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...