import numpy as np
def convert(raw_path, target_path):
# 先以uint8加载
ref_8bit = np.fromfile(raw_path, dtype=np.uint8)
ref_bits_unpack = np.unpackbits(ref_8bit, bitorder='little').reshape(-1, 20) # 20bit一个数据
# 高位补0
zero_bits = 32 - 20
zero_cols = np.zeros((ref_bits_unpack.shape[0], zero_bits), dtype=ref_bits_unpack.dtype)
ref_32bit_unpack = np.hstack((zero_cols, ref_bits_unpack))
# pack并转为uint32表示
ref_32bit_packed = np.packbits(ref_32bit_unpack, bitorder='little').astype(np.uint32)
# 获取高低位,拼凑正真正的uint32
ref_0b_7b = ref_32bit_packed[0::4]
ref_8b_15b = ref_32bit_packed[1::4]
ref_16b_23b = ref_32bit_packed[2::4]
ref_24b_31b = ref_32bit_packed[3::4]
ref_32bit = ref_0b_7b | (ref_8b_15b << 8) | (ref_16b_23b << 16) | (ref_24b_31b << 24)
ref_32bit.tofile(target_path)
if __name__ == '__main__':
convert('./input_rggb_20bit.raw', './input_rggb_32bit.raw')