Python读取raw文件

rawData = open("foo.raw" 'rb').read()
imgSize = (x,y) ###The size of the image
# Use the PIL raw decoder to read the data.
# the 'F;16' informs the raw decoder that we are reading 
# a little endian, unsigned integer 16 bit data.
img = Image.fromstring('L', imgSize, rawData, 'raw')
img.save("foo.png")

Using the handbook and another SO question.

The First argument is the image mode and can be any from:

  • 1 (1-bit pixels, black and white, stored with one pixel per byte)
  • L (8-bit pixels, black and white)
  • P (8-bit pixels, mapped to any other mode using a colour palette)
  • RGB (3x8-bit pixels, true colour)
  • RGBA (4x8-bit pixels, true colour with transparency mask)
  • CMYK (4x8-bit pixels, colour separation)
  • YCbCr (3x8-bit pixels, colour video format)
  • I (32-bit signed integer pixels)
  • F (32-bit floating point pixels)

 

链接:https://stackoverflow.com/questions/9922402/how-to-convert-raw-images-to-png-in-python

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用Python读取raw文件,可以使用numpy库中的fromfile函数读取文件,并使用reshape函数将数据重新排列。以下是一个示例代码: ```python import numpy as np def readRaw(path): imgData = np.fromfile(path, dtype='uint8') rows = 512 # 图像的行数 cols = 640 # 图像的列数 channels = 1 # 图像的通道数,灰度图为1 imgData = imgData.reshape(rows, cols, channels) return imgData path = 'path_to_your_raw_file.raw' img = readRaw(path) ``` 在上述代码中,readRaw函数接受一个文件路径作为参数,并使用numpy的fromfile函数读取raw文件。然后,通过reshape函数将数据重新排列成图片的形状,最后返回读取到的图像数据。 请注意,上述代码是一个示例,你需要将'path_to_your_raw_file.raw'替换为你要读取的实际raw文件的路径。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python——读取raw格式文件](https://blog.csdn.net/weixin_42686816/article/details/126874854)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [lena.raw图片文件下载及打开方式](https://blog.csdn.net/lic1697067085/article/details/116353553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [RAW图像详解及使用Python读取raw格式图像并显示](https://blog.csdn.net/m0_46378271/article/details/126090531)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值