2

我正在尝试使用 python 将原始图像数据转换为 png。我对python很陌生,尤其是对图像处理...

原始文件是 16 位灰度图像。

因为我已经扫描了论坛,所以我想出了以下解决方案:

from PIL import Image, ImageFilter
import numpy as np
from scipy import ndimage, misc


rawfile = np.fromfile('test.raw', dtype=np.int16)

rawfile.shape = (1025,1025)
imgSize = (1025,1025)

img = Image.fromstring('L', imgSize, rawfile, 'raw', 'F;16')
img.save("rawData.png")

但我不断收到以下错误:

Traceback (most recent call last):
  File "****\Programs\Python 2.7.6\readraw\readraw.py", line 11, in <module>
    img = Image.fromstring('L', imgSize, rawfile, 'raw', 'F;16')
  File "****\Programs\Python 2.7.6\lib\site-packages\PIL\Image.py", line 1835, in fromstring
    return frombytes(*args, **kw)
  File "****\Programs\Python 2.7.6\lib\site-packages\PIL\Image.py", line 1822, in frombytes
    im.frombytes(data, decoder_name, args)
  File "****\Programs\Python 2.7.6\lib\site-packages\PIL\Image.py", line 608, in frombytes
    d = _getdecoder(self.mode, decoder_name, args)
  File "****\Programs\Python 2.7.6\lib\site-packages\PIL\Image.py", line 390, in _getdecoder
    return decoder(mode, *args + extra)
ValueError: unknown raw mode

有人可以向我解释为什么原始模式未知吗?我检查了文档,据我了解该主题,枕头库应该附带这个?!

最好的祝福


谢谢你们的帮助!

我改变了我的代码,现在它似乎工作了:

from PIL import Image, ImageFilter
import numpy as np
from scipy import ndimage, misc


rawfile = np.fromfile('test.raw', "uint16")

rawfile.shape = (1025,1025)
misc.imsave("test.png", rawfile)
4

3 回答 3

0

我不认为 PIL 理解 numpy 对象。我认为 PIL Image 有一种从磁盘读取原始文件的方法。然后您可以修改内存中的图像并以您喜欢的格式保存/导出它。

于 2014-02-21T20:28:22.490 回答
0

PIL(和 PILLOW)不能直接使用 numpy 数组。在numpy中有一个来回转换的功能,但我倾向于不使用PIL来处理这种东西。

相反,我建议您使用 保存数组scipy.misc.imsave(),此函数可以保存为 PNG。

于 2014-02-21T20:32:22.760 回答
0

使用raw2png.py?

来源:http://www.cl.cam.ac.uk/~cs448/git/trunk/src/bin/raw2png.py

或带有 exec 命令行的 ImageMagick convert:?

于 2014-02-21T20:38:27.353 回答