python应用 |
您所在的位置:网站首页 › lr批量raw转jpg › python应用 |
例一:
一、要求
读入0.raw的文件,转化为0.jpg文件。 二、代码 import numpy as np import imageio rawfile = np.fromfile('0.raw', dtype=np.float32) # 以float32读图片 print(rawfile.shape) rawfile.shape = (480, 640) print(rawfile.shape) b=rawfile.astype(np.uint8)#变量类型转换,float32转化为int8 print(b.dtype) imageio.imwrite("0.jpg", b) import matplotlib.pyplot as pyplot pyplot.imshow(rawfile) pyplot.show() 三、运行结果这里有个问题:将黑白两通道的图像放到了pyplot三通道中,导致了奇怪的颜色。 例二: 一、要求从jpg图像生成raw图像,对该raw图像进行显示 二、代码 import numpy as np import cv2 img = cv2.imread('12.jpg') # 根据jpg生成raw img.tofile('12.raw') type = img.dtype#得到数据格式,如uint8和uint16等 width, height, channels = img.shape# 得到图像大小和通道数 print(type,width, height, channels) # 利用numpy.fromfile函数读取raw文件,并指定数据格式 imgData = np.fromfile('12.raw', dtype=type) # 将读取到的数据进行重新排列,从一维变为三维 imgData = imgData.reshape(width, height, channels) # 方法一:使用cv2展示图像 # cv2.imshow('img',imgData) # cv2.waitKey() # cv2.destroyAllWindows() #方法二:使用matplotlib展示图像 import matplotlib.pyplot as pyplot#cv2中的色彩排列是(b,g,r),而matplotlib库中的排列方式是(r,g,b) imgData_rgb=imgData[:,:,::-1]#修改色彩排列 pyplot.imshow(imgData_rgb) pyplot.savefig('667.jpg')#将当前图像保存为一张jpg图像,放在.show后面会变成空图 pyplot.show() 三、运行结果这篇是初学cv2写的,建议看后来写的另一篇,可以只用cv2和numpy实现: https://blog.csdn.net/weixin_41819299/article/details/82428950 扩展阅读:如何将Numpy数组保存为图像 https://blog.csdn.net/zhuoyuezai/article/details/79635120 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |