[Numpy-discussion] import 16-bit tiff - byte-order problem?
Thu Nov 6 22:15:14 CST 2008
2008/11/6 Robert Kern <email@example.com>:
> On Thu, Nov 6, 2008 at 21:54, Angus McMorland <firstname.lastname@example.org> wrote:
>> Hi all,
>> I'm trying to import a 16-bit 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
>> non-zero 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?
> 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
> Numpy-discussion mailing list
Post-doctoral research fellow
Neurobiology, University of Pittsburgh
More information about the Numpy-discussion