Question

I'm trying to convert raw image data into png with python. I'm pretty new to python and especially to image processing...

The raw file is a 16 bit greyscale image.

As I already scanned the forums I came up with the following solution:

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")

But I keep getting the following errors:

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

Can someone explain to me why the raw mode is unknown? I checked the Docs and as far as I understand the topic, the PILLOW lib should come with this?!

Best regards


Thank you guys for your help!

I altered my code and now it seems to work:

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)
Was it helpful?

Solution

PIL (and PILLOW) don't work with numpy arrays directly. There is a function to convert back and forth in numpy, but I tend not to use PIL for this kind of stuff.

Instead, I propose you save your array with scipy.misc.imsave(), this function can save to a PNG.

OTHER TIPS

I don't think PIL understands numpy objects. I would think PIL Image has a method to read the raw file from disk. Then you can modify the image in memory and save/export it in the format you like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top