Numpy 将 uint16 解包为 1-5-5-5 位块

我正在尝试使用 numpy 将二进制字符串转换为 Python 中的图像,但我很难找到一种使用非常规位分布处理它的好方法(据我所知)。


这些是如何转换以及转换什么的细节。16 位纹理图块 (256*256)。每个 bitu16 代表一个像素,其颜色的形式为 ARGB,MSB-to-LSB: 1 位透明度

5 位红色通道 5 位绿色通道 5 位蓝色通道


Numpy 并不真正支持任何 1 位或 5 位的东西。我尝试使用不同的 argb 通道设置 np.dtype 但没有任何运气。unpackbits 似乎不适用于 uint16 所以在那种情况下我可能不得不将它分成 2 个 uint8



dt = np.dtype([('a', np.bool_), ('r', np.half), ('g', np.half), ('b', np.half)])


data = data.read(131072)


dataFromBuffer = np.frombuffer(data, dtype=dt)

img  = dataFromBuffer.reshape(256, 256)


哆啦的时光机
浏览 213回答 2
2回答

米琪卡哇伊

在这种情况下,您对缺乏 bit numpy 位级别支持是正确的。可以按如下方式处理位的高级(但功能)方法:image_16_bit = 123 # A 16bit integer.bits = '{:016b}'.format(image_16_bit) transparency = int(bits[0], 2)red_channel = int(bits[1:6], 2)green_channel = int(bits[6:11], 2)blue_channel = int(bits[11:], 2)print(transparency, red_channel, green_channel, blue_channel) # 0 0 3 27您可以对所有整数运行此操作,然后收集各个通道值。最后,您可以将其转换为一个 numpy 数组,以将您的图像作为一个 numpy 数组。

幕布斯7119047

以下是如何使您的方法奏效:# make small examplex = np.random.randint(0,1<<16,size=(5,5),dtype=np.uint16)# set up dtypedt = np.dtype([*zip('argb',(bool,*3*(np.uint8,)))])# bit of bit twiddlingdef unpack_argb(x):&nbsp; &nbsp; out = np.empty(x.shape,dt)&nbsp; &nbsp; for i,ch in enumerate(reversed('argb')):&nbsp; &nbsp; &nbsp; &nbsp; out[ch] = (x>>(5*i))&31&nbsp; &nbsp; return outdef pack_argb(x):&nbsp; &nbsp; out = x['a'].astype(np.uint16)&nbsp; &nbsp; for ch in 'rgb':&nbsp; &nbsp; &nbsp; &nbsp; out <<= 5&nbsp; &nbsp; &nbsp; &nbsp; out += x[ch]&31&nbsp; &nbsp; return out# check round tripnp.all(x == pack_argb(unpack_argb(x)))# True更新:def argb16_to_rgba32(x):&nbsp; &nbsp; out = np.empty(x.shape+(4,),np.uint8)&nbsp; &nbsp; out[...,3] = (x>>8)&0x80&nbsp; &nbsp; out[...,0] = (x>>7)&0xf8&nbsp; &nbsp; out[...,1] = (x>>2)&0xf8&nbsp; &nbsp; out[...,2] = (x<<3)&0xf8&nbsp; &nbsp; return outdef rgba32_to_argb16(x):&nbsp; &nbsp; x16 = x.astype(np.uint16)&0xf8&nbsp; &nbsp; out = (x16[...,3]&0x80)<<8&nbsp; &nbsp; out += x16[...,0]<<7&nbsp; &nbsp; out += x16[...,1]<<2&nbsp; &nbsp; out += x16[...,2]>>3&nbsp; &nbsp; return out
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python