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