python读取raw图片文件_[python_Load RAW数据并显示]

下面是一个简单的将raw图显示出来的python代码

代码中所使用的raw图上传不了,如有需要,请留邮箱

代码如有错,烦请指出,谢谢

import cv2

from PIL import Image as im

import numpy as np

class pyOpenCvTest():

'''

author:Mars

function:将1600x1200的raw图显示出来

res:cwf__1600x1200_10_3.raw

step1:

将raw数据通过numpy的fromfile()函数以uint16读入,读入后是一个size为1600x1200的1维数组

Tip:因为raw数据是AD10bit,所以要用uint16

step2:

将数组reshape成1200x1600,因为图像是按1200x1600结构存储的

step3:

将图像从10bit转成8bit。

Question:为什么要转成8bit输出

10bit输出时画面PIL.image.show()会过曝,猜测show()是按YUV 8bit显示的。

step4:

通过PIL的image类的fromarray()函数将数组转成图像输出

'''

rawPath = r"G:\OpenCvTest\pyOpenCvTest\res\cwf__1600x1200_10_3.raw"

imgSize = (1200,1600)

def cvTest():

#step1:

rawData = np.fromfile(pyOpenCvTest.rawPath,dtype = 'uint16')

print(np.shape(rawData)) #1920000

#step2:

reshapeRawData = np.reshape(rawData,pyOpenCvTest.imgSize)

#step3:

for i in range(reshapeRawData.shape[0]):

for j in range(reshapeRawData.shape[1]):

#将10bit数转成8bit

val = np.round(reshapeRawData[i][j] >> 2)

if val >= 255:

reshapeRawData[i][j] = 255

elif val <= 0:

reshapeRawData[i][j] = 0

else:

reshapeRawData[i][j] = val

print(reshapeRawData.max())#200

print(reshapeRawData.min())#3

img = im.fromarray(reshapeRawData)

img.show()

显示结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值