weird matplotlib imread question for png

Firstly, thanks, Fabrice Silva

I have checked my picture files again.

For python-gray.png, now it is attacched here or can be downloaded
from http://bbs.blendercn.org/data/attachment/forum/201504/16/222351w3952n3o9968m9a5.png.
xnview says it is 128*128*8, but "print
imread('python-gray.png').shape" says (128, 128, 3), however I suppose
it should be (128, 128)!

For python-color.png, it is my fault. xnview says it is 128*128*32, so
it has alpha channel. Hence "imread().shape =(128, 128, 4)" is right

btw. imread return array which has value between 0 and 1 for PNG file.
But for other picture format, the value is 0~255. The manual says
matplotlib reads PNG only by it self, and other files via PIL.But I
think it is better to make the returned array consistent.

python-gray.png

xnview says it is 1281288, but “print
imread(‘python-gray.png’).shape” says (128, 128, 3), however I suppose
it should be (128, 128)!
Not sure that this is true, but I guess that xnview is using the third dimension here to refer to a number of bytes. In this case, it is two bytes, one for the black/white level and the other for alpha level. When you import this with imread, the png is converted into a Numpy array. The default behavior in this case is to create an array which is 1281283 because the third dimmension is the (R,G,B) levels, which will all be equal for a gray scale image. This behavior is probably intentional so that you don’t have to write different code to handle gray/color images.

For python-color.png, it is my fault. xnview says it is 12812832, so
it has alpha channel. Hence “imread().shape =(128, 128, 4)” is right
If the third dimension from xnview is bytes, then yes, you are correct.

btw. imread return array which has value between 0 and 1 for PNG file.
But for other picture format, the value is 0~255. The manual says
matplotlib reads PNG only by it self, and other files via PIL.But I
think it is better to make the returned array consistent.
That is most likely due to the way that PNG and e.g. older JPG are defined. PNG defines each RGBA value using float32, while older JPG uses uint8. Therefor, it would not make sense to change the dtype of the image on import.

Hope that helps.

Ryan

···

On Thu, Apr 16, 2015 at 10:51 AM, oyster <lepto.python@…287…> wrote:

Firstly, thanks, Fabrice Silva

I have checked my picture files again.

For python-gray.png, now it is attacched here or can be downloaded

from http://bbs.blendercn.org/data/attachment/forum/201504/16/222351w3952n3o9968m9a5.png.

xnview says it is 1281288, but "print

imread(‘python-gray.png’).shape" says (128, 128, 3), however I suppose

it should be (128, 128)!

For python-color.png, it is my fault. xnview says it is 12812832, so

it has alpha channel. Hence “imread().shape =(128, 128, 4)” is right

btw. imread return array which has value between 0 and 1 for PNG file.

But for other picture format, the value is 0~255. The manual says

matplotlib reads PNG only by it self, and other files via PIL.But I

think it is better to make the returned array consistent.


BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT

Develop your own process in accordance with the BPMN 2 standard

Learn Process modeling best practices with Bonita BPM through live exercises

http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Funnily (or not), the png is in fact 8-bits depth so you could infer it
is grayscale, but matplotlib is right : your file uses a color palette.
The 8-bits values refer to indices of the 256-length color palette.
These indices are then converted back to the colorspace, so the shape is
eventually (...,3). Unfortunately, the color palette is here the
grayscale...

···

Le jeudi 16 avril 2015, oyster a écrit :

Firstly, thanks, Fabrice Silva

I have checked my picture files again.

For python-gray.png, now it is attacched here or can be downloaded
from http://bbs.blendercn.org/data/attachment/forum/201504/16/222351w3952n3o9968m9a5.png.
xnview says it is 128*128*8, but "print
imread('python-gray.png').shape" says (128, 128, 3), however I suppose
it should be (128, 128)!

--
Fabrice

Oops. I meant bits not bytes in my earlier statements. Sorry.

···

On Thu, Apr 16, 2015 at 11:24 AM, Ryan Nelson <rnelsonchem@…287…> wrote:

xnview says it is 1281288, but “print
imread(‘python-gray.png’).shape” says (128, 128, 3), however I suppose
it should be (128, 128)!
Not sure that this is true, but I guess that xnview is using the third dimension here to refer to a number of bytes. In this case, it is two bytes, one for the black/white level and the other for alpha level. When you import this with imread, the png is converted into a Numpy array. The default behavior in this case is to create an array which is 1281283 because the third dimmension is the (R,G,B) levels, which will all be equal for a gray scale image. This behavior is probably intentional so that you don’t have to write different code to handle gray/color images.

For python-color.png, it is my fault. xnview says it is 12812832, so
it has alpha channel. Hence “imread().shape =(128, 128, 4)” is right
If the third dimension from xnview is bytes, then yes, you are correct.

btw. imread return array which has value between 0 and 1 for PNG file.
But for other picture format, the value is 0~255. The manual says
matplotlib reads PNG only by it self, and other files via PIL.But I
think it is better to make the returned array consistent.
That is most likely due to the way that PNG and e.g. older JPG are defined. PNG defines each RGBA value using float32, while older JPG uses uint8. Therefor, it would not make sense to change the dtype of the image on import.

Hope that helps.

Ryan

On Thu, Apr 16, 2015 at 10:51 AM, oyster <lepto.python@…287…> wrote:

Firstly, thanks, Fabrice Silva

I have checked my picture files again.

For python-gray.png, now it is attacched here or can be downloaded

from http://bbs.blendercn.org/data/attachment/forum/201504/16/222351w3952n3o9968m9a5.png.

xnview says it is 1281288, but "print

imread(‘python-gray.png’).shape" says (128, 128, 3), however I suppose

it should be (128, 128)!

For python-color.png, it is my fault. xnview says it is 12812832, so

it has alpha channel. Hence “imread().shape =(128, 128, 4)” is right

btw. imread return array which has value between 0 and 1 for PNG file.

But for other picture format, the value is 0~255. The manual says

matplotlib reads PNG only by it self, and other files via PIL.But I

think it is better to make the returned array consistent.


BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT

Develop your own process in accordance with the BPMN 2 standard

Learn Process modeling best practices with Bonita BPM through live exercises

http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users