Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python将原始图像转换为png_Python_Pillow - Fatal编程技术网

如何使用python将原始图像转换为png

如何使用python将原始图像转换为png,python,pillow,Python,Pillow,我正在尝试使用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,102

我正在尝试使用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

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

PIL(和PILLOW)不直接使用numpy阵列。在numpy中有一个来回转换的函数,但我倾向于不将PIL用于这类东西


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

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


或者带有exec命令行的ImageMagick:
转换

tbh此源代码真让我困惑。。。我试图阅读和理解它,但作为一个初学者,这是一项相当艰巨的任务:)这也是我最初的想法,但我找不到他们文档中列出的任何方法。我找到的唯一一件事就是编写自己的解码器
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)