<div dir="ltr"><div><div>What behavior is unexpected?  For the (256, 256) images, matplotlib applies its default colormap to the grayscale (v1.5 and previous, that is jet, +v2.0, that will be viridis). The numpy array as loaded from PIL will never carry any additional information that came from the TIFF.<br><br></div>As for PIL, it will return an RGB[A] array if there is colormap data in the TIFF. If there is no colormap specified in the TIFF, it'll give you a simple 2D array. Now, maybe you'd like it to always return an RGB[A] array, but without a colormap in the TIFF, it makes sense to return the data as-is. This makes sense for people treating the TIFF as a data format rather than a visualization data format.<br><br></div>Ben Root<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 29, 2016 at 12:47 PM, Henrique Almeida <span dir="ltr"><<a href="mailto:hdante.lnls@gmail.com" target="_blank">hdante.lnls@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> I think in any case, the result is unexpected, PIL is loading garbage<br>
from memory when loading black and white images because it sends the<br>
wrong buffer size, and matplotlib correctly loads the black and white<br>
image, but stores it in a 3D array.<br>
<div class="HOEnZb"><div class="h5"><br>
2016-04-29 13:43 GMT-03:00 Henrique Almeida <<a href="mailto:hdante.lnls@gmail.com">hdante.lnls@gmail.com</a>>:<br>
>  For 1 bit images, the resulting array has shape (256, 256, 4). For<br>
> grayscale images, the shape is (256, 256). So the image seems to have<br>
> been loaded as a color image.<br>
><br>
> 2016-04-29 13:38 GMT-03:00 Benjamin Root <<a href="mailto:ben.v.root@gmail.com">ben.v.root@gmail.com</a>>:<br>
>> What kind of array is "img"? What is its dtype and shape?<br>
>><br>
>> plt.imshow() will use the default colormap for matplotlib if the given array<br>
>> is just 2D. But if it is 3D (a 2D array of RGB[A] channels), then it will<br>
>> forego the colormap and utilize that for the colors. It knows nothing of the<br>
>> colormap contained in the TIFF.<br>
>><br>
>> Ben Root<br>
>><br>
>><br>
>> On Fri, Apr 29, 2016 at 12:31 PM, Henrique Almeida <<a href="mailto:hdante.lnls@gmail.com">hdante.lnls@gmail.com</a>><br>
>> wrote:<br>
>>><br>
>>>  Paul, yes, imread() worked for reading the black and white TIFF. The<br>
>>> situation improved, but now, there seems to be some problem with the<br>
>>> color map. Example code:<br>
>>><br>
>>> #!/usr/bin/env python3<br>
>>> import numpy<br>
>>> from matplotlib import pyplot, cm<br>
>>><br>
>>> img = pyplot.imread('oi-00.tiff')<br>
>>> pyplot.imshow(img)<br>
>>> pyplot.colorbar()<br>
>>> pyplot.show()<br>
>>><br>
>>>  The code can open both 1-bit and 8-bit images, but only with 8 bits<br>
>>> the image is shown with the colormap colors. The 1 bit image is shown<br>
>>> as black and white.<br>
>>><br>
>>>  The questions:<br>
>>>  1) Should Image.open() behave like pyplot.imread() ? Is this a bug in PIL<br>
>>> ?<br>
>>>  2) Why isn't the colormap working with black and white images ?<br>
>>><br>
>>> 2016-04-29 13:06 GMT-03:00 Paul Hobson <<a href="mailto:pmhobson@gmail.com">pmhobson@gmail.com</a>>:<br>
>>> > Does using pyplot.imgread work?<br>
>>> ><br>
>>> > On Fri, Apr 29, 2016 at 8:27 AM, Henrique Almeida<br>
>>> > <<a href="mailto:hdante.lnls@gmail.com">hdante.lnls@gmail.com</a>><br>
>>> > wrote:<br>
>>> >><br>
>>> >>  Any help with this problem ?<br>
>>> >><br>
>>> >> 2016-04-27 11:35 GMT-03:00 Henrique Almeida <<a href="mailto:hdante.lnls@gmail.com">hdante.lnls@gmail.com</a>>:<br>
>>> >> >  Hello, what's the current status on numpy for loading bit-arrays ?<br>
>>> >> ><br>
>>> >> > I'm currently unable to correctly load black and white (1-bit) TIFF<br>
>>> >> > images. Code example follows:<br>
>>> >> ><br>
>>> >> > from PIL import Image<br>
>>> >> > import numpy<br>
>>> >> > from matplotlib import pyplot<br>
>>> >> ><br>
>>> >> > img = Image.open('oi-00.tiff')<br>
>>> >> > a = numpy.array(img)<br>
>>> >> ><br>
>>> >> > ^ does not work for 1-bit TIFF images<br>
>>> >> ><br>
>>> >> > PIL source shows that it incorrectly uses typestr == '|b1'. I tried<br>
>>> >> > to<br>
>>> >> > change this to '|t1', but I get :<br>
>>> >> ><br>
>>> >> > TypeError: data type "|t1" not understood<br>
>>> >> ><br>
>>> >> > My goal is to make the above code to work for black and white TIFF<br>
>>> >> > images the same way it works for grayscale images. Any help ?<br>
>>> >> _______________________________________________<br>
>>> >> NumPy-Discussion mailing list<br>
>>> >> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
>>> >> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
>>> ><br>
>>> ><br>
>>> ><br>
>>> > _______________________________________________<br>
>>> > NumPy-Discussion mailing list<br>
>>> > <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
>>> > <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
>>> ><br>
>>> _______________________________________________<br>
>>> NumPy-Discussion mailing list<br>
>>> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
>>> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
>><br>
>><br>
>><br>
>> _______________________________________________<br>
>> NumPy-Discussion mailing list<br>
>> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
>> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
>><br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br></div>