[Numpy-discussion] import 16-bit tiff - byte-order problem?

Robert Kern robert.kern@gmail....
Thu Nov 6 21:58:42 CST 2008


On Thu, Nov 6, 2008 at 21:54, Angus McMorland <amcmorl@gmail.com> 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.

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


More information about the Numpy-discussion mailing list