【问题标题】:open .raw image data using python使用 python 打开 .raw 图像数据
【发布时间】:2015-09-07 13:24:35
【问题描述】:

我一直在谷歌搜索使用 python 库显示原始图像数据的方法,但找不到任何合适的解决方案。数据取自相机模块,扩展名为“.raw”。此外,当我尝试通过“更多文件名.raw”在终端中打开它时,控制台说这是一个二进制文件。供应商告诉我,相机输出 16 位原始灰度数据。

但我想知道如何通过 PIL、Pillow 或仅通过 Numpy 显示这些数据。我已经测试了 PIL 的 Image 模块。但是,它无法识别图像数据文件。 PIL 似乎没有将 .raw 文件视为图像数据格式。 BMP 文件可以显示,但是这个 '.raw' 不能。

当我尝试使用 read 函数和 matplotlib 时,如下所示

from matplotlib import pyplot as plt
f = open("filename.raw", "rb").read() 
plt.imshow(f) 
plt.show()

然后发生错误

错误:图像数据无法转换为浮点数

任何想法将不胜感激。

链接: camera module

我用以下代码做了一些改进。但现在的问题是这段代码只显示了整个图像的一部分。

from matplotlib import pyplot as plt
import numpy as np
from StringIO import StringIO
from PIL import *
scene_infile = open('G0_E3.raw','rb')
scene_image_array = np.fromfile(scene_infile,dtype=np.uint8,count=1280*720)
scene_image = Image.frombuffer("I",[1280,720],
                                 scene_image_array.astype('I'),
                                 'raw','I',0,1)
plt.imshow(scene_image)
plt.show()

【问题讨论】:

  • RAW 不是位图或 jpeg 意义上的图像格式。这些图像包含每个像素的颜色,例如 rgb 值。 RAW 基本上包括照片船的原始数据,尚未转换为颜色值(因此是 16 位灰度)。这就是为什么(半)专业摄影师经常使用它来改变颜色在后期处理中的显示方式的原因。由于尚未完成将数据转换为每像素颜色的步骤,所有标准图片库很可能无法读取 RAW 数据格式。
  • 你搜索过 numpy 和 RAW 吗?我记得 RAW 有一个品牌特定的标题块。
  • 你可能想看看rawkit。我从未使用过它,因此无法评论它对您的用处,但看起来它在这里可能会有所帮助
  • 还有一个程序(和库)可以读取和转换来自多个相机型号的 RAW 图像,请参阅cybercom.net/~dcoffin/dcraw
  • 感谢您的所有回答。但我想了解如何通过 python 手动打开和操作原始图像。而且我应该删除'from StringIO ...'行。我现在不用。

标签: numpy python-imaging-library


【解决方案1】:

看看rawpy:

import rawpy
import imageio

path = 'image.raw'
raw = rawpy.imread(path)
rgb = raw.postprocess()
imageio.imsave('default.tiff', rgb)

rgb 只是一个 RGB numpy 数组,因此您可以使用任何库(不仅仅是imageio)将其保存到磁盘。

如果您想访问未处理的拜耳数据,请执行以下操作:

bayer = raw.raw_image

另请参阅API docs

【讨论】:

    【解决方案2】:
    import numpy as np
    from PIL import Image
    import rawpy
    
    
    # For Adobe DNG image
    input_file = 'path/to/rawimage.dng'
    rawimg = rawpy.imread(input_file)
    npimg = rawimg.raw_image
    
    
    # For raw image (without any headers)
    input_file = 'path/to/rawimage.raw'
    npimg = np.fromfile(input_file, dtype=np.uint16)
    imageSize = (3648, 2736)
    npimg = npimg.reshape(imageSize)
    
    
    # Save the image from array to file.
    # TIFF is more suitable for 4 channel 10bit image
    # comparing to JPEG
    output_file = 'out.tiff'
    Image.fromarray(npimg/1023.0).save(output_file)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多