Hi all,
I'm trying to import a 16bit tiff image into a numpy array. I have found, using google, suggestions to do the following:
After starting with: i = Image.open('16bitGreyscaleImage.tif')
Stéfan van der Walt suggested: a = np.array(i.getdata()).reshape(i.size) # a 1d numpy array
and adapted from Nadav Horesh's suggestion: a = np.fromstring(i.tostring(), dtype=np.uint16).reshape(256, 256)
Both give me the same answer as: a = np.array(i, dtype=np.uint16)
In all cases it looks like the resulting byte order is wrong: pixels with 0 values correctly are 0 in a, in the correct places, but all nonzero values are wrong compared to the same image opened in ImageJ (in which the image looks correct).
What's the conversion magic I need to invoke to correctly intepret this image type?
Thanks,
Angus.
On Thu, Nov 6, 2008 at 21:54, Angus McMorland amcmorl@gmail.com wrote:
Hi all,
I'm trying to import a 16bit tiff image into a numpy array. I have found, using google, suggestions to do the following:
After starting with: i = Image.open('16bitGreyscaleImage.tif')
Stéfan van der Walt suggested: a = np.array(i.getdata()).reshape(i.size) # a 1d numpy array
and adapted from Nadav Horesh's suggestion: a = np.fromstring(i.tostring(), dtype=np.uint16).reshape(256, 256)
Both give me the same answer as: a = np.array(i, dtype=np.uint16)
This is the preferred way to do it these days.
In all cases it looks like the resulting byte order is wrong: pixels with 0 values correctly are 0 in a, in the correct places, but all nonzero values are wrong compared to the same image opened in ImageJ (in which the image looks correct).
What's the conversion magic I need to invoke to correctly intepret this image type?
Hmm, it is possible that the __array_interface__ is giving you the wrong endian information. Anyways, use a.byteswap() to get a view of the array with the other endianness.
2008/11/6 Robert Kern robert.kern@gmail.com:
On Thu, Nov 6, 2008 at 21:54, Angus McMorland amcmorl@gmail.com wrote:
Hi all,
I'm trying to import a 16bit tiff image into a numpy array. I have found, using google, suggestions to do the following:
After starting with: i = Image.open('16bitGreyscaleImage.tif')
Stéfan van der Walt suggested: a = np.array(i.getdata()).reshape(i.size) # a 1d numpy array
and adapted from Nadav Horesh's suggestion: a = np.fromstring(i.tostring(), dtype=np.uint16).reshape(256, 256)
Both give me the same answer as: a = np.array(i, dtype=np.uint16)
This is the preferred way to do it these days.
In all cases it looks like the resulting byte order is wrong: pixels with 0 values correctly are 0 in a, in the correct places, but all nonzero values are wrong compared to the same image opened in ImageJ (in which the image looks correct).
What's the conversion magic I need to invoke to correctly intepret this image type?
Hmm, it is possible that the __array_interface__ is giving you the wrong endian information. Anyways, use a.byteswap() to get a view of the array with the other endianness.
Many thanks Robert, that did the trick. I looked through the new numpy reference for something like the byteswap function, but I see now it's less obvious since it's only an array instance method and not a function, for which the documentation is more systematic.
Should there be a change made somewhere to document or accommodate the other endianness of the images?
Angus.
 Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth."  Umberto Eco _______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
Hi,
I need to perform iir filter operation using numpy and could not googled any useful info for this. Are there filter operation similar the matlab filter funciton in Numpy?
Thanks
Frank _________________________________________________________________ Color coding for safety: Windows Live Hotmail alerts you to suspicious email. http://windowslive.com/Explore/Hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_safety_...
On Fri, Nov 7, 2008 at 00:25, frank wang f.yw@hotmail.com wrote:
Hi,
I need to perform iir filter operation using numpy and could not googled any useful info for this. Are there filter operation similar the matlab filter funciton in Numpy?
Not in numpy. scipy.signal.lfilter() does, though.
On 6Nov08, at 11:15 PM, Angus McMorland wrote:
2008/11/6 Robert Kern robert.kern@gmail.com:
On Thu, Nov 6, 2008 at 21:54, Angus McMorland amcmorl@gmail.com wrote:
Hi all,
I'm trying to import a 16bit tiff image into a numpy array. I have found, using google, suggestions to do the following:
After starting with: i = Image.open('16bitGreyscaleImage.tif')
Stéfan van der Walt suggested: a = np.array(i.getdata()).reshape(i.size) # a 1d numpy array
As an aside, if you have matplotlib installed, you might be able to sidestep this problem completely with matplotlib.image.pil_to_array.
Cheers,
David
Hi,
The PIL has some fundamental architectural problems that prevent it from dealing easily with 16bit TIFFs, which are exacerbated on little endian platforms. Add to this a thin sheen of various byteorder bugs and other problems in the __array_interface__, and it's really hard to get consistent loading of 16bit tiffs across platforms. (I and many others have submitted patches for these issues to no avail.)
A while ago, I tried to see if I could graft the image file format parsers from the PIL onto a byteloading backend that used numpy. Unfortunately, I really couldn't  the parsers assume too much about the problematic architecture of the PIL.
I do have a private fork of the PIL that I made which remedies the bugs and abovementioned architectural issuse, and works cross platform, and with any endian system. (It's very restricted compared to the regular PIL  basically it just does image IO and then converts to numpy arrays.) I haven't released this because I don't really want to make trouble  and we're promised that a major revision of the PIL is in the offing which will fix these troubles  but I'm happy to send the code out to those who actually need reliable 16bit image IO.
Zach
participants (5)

Angus McMorland

David WardeFarley

frank wang

Robert Kern

Zachary Pincus