python二进制图片压缩传输_Python:读取12位压缩的二进制图像

I have a 12 bit packed image from a GigE camera. It is a little-endian file and each 3 bytes hold 2 12-bit pixels.

I am trying to read this image using python and I tried something like this:

import bitstring

import numpy

with open('12bitpacked1.bin', 'rb') as f:

data = f.read()

ii=numpy.zeros(2*len(data)/3)

ic = 0

for oo in range(0,len(data)/3):

aa = bitstring.Bits(bytes=data[oo:oo+3], length=24)

ii[ic],ii[ic+1] = aa.unpack('uint:12,uint:12')

ic=ic+2

b = numpy.reshape(ii,(484,644))

In short: I read 3 bytes, convert them to bits and then unpack them as two 12-bit integers.

The result is, however, very different from what it should be. It looks like the image is separated into four quarters, each of them expanded to full image size and then overlapped.

What am I doing wrong here?

Update: Here are the test files:

They will not be identical, but they should show the same image. 12-bit normal has 12-bit pixel as uint16.

with open('12bit1.bin', 'rb') as f:

a = numpy.fromfile(f, dtype=numpy.uint16)

b = numpy.reshape(a,(484,644))

解决方案

With this code

for oo in range(0,len(data)/3):

aa = bitstring.Bits(bytes=data[oo:oo+3], length=24)

you are reading bytes data[0:3], data[1:4], ... What you actually want is probably this:

for oo in range(0,len(data)/3):

aa = bitstring.Bits(bytes=data[3*oo:3*oo+3], length=24)

[EDIT]

Even more compact would be this:

for oo in range(0,len(data)-2,3):

aa = bitstring.Bits(bytes=data[oo:oo+3], length=24)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值