我試圖用python將原始圖像數據轉換爲png。 我很新的python,尤其是圖像處理...如何使用python將原始圖像轉換爲png
原始文件是一個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
有人能向我解釋爲什麼原始模式未知?我檢查了Docs,並且據我瞭解這個主題,PILLOW lib應該帶這個?!
問候
謝謝你們的幫助!
我改變了我的代碼,現在它似乎工作:
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)
這就是我第一次想到的,但我找不到在他們的文檔中列出的任何方法。我發現的唯一的東西是和教程代碼自己的解碼器 – user3338564