Pil Import Png Pixels As Single Value Instead Of 3 Values Vector
I have a bunch of map files I've downloaded from Google maps in png formats that I want to convert to a single larger image. When I import one and I look at the pixels, I see that
Solution 1:
The reason of such result (value 153 in [0,0]
) is that image mode is set to P
(8-bit pixels, mapped to any other mode using a colour palette). If You want to set different mode (e.g. RGB
) You can do it before invoking method load()
.
Here is an example of how to do this:
from PIL import Image
file_data = Image.open('b1.png')
file_data = file_data.convert('RGB') # conversion to RGB
data = file_data.load()
print data[0,0]
and the result of print is
(240, 237, 229)
For more information about Image Modes please visit the documentation.
Solution 2:
Your image is in mode=P
. It has it's colors defined in a color palette.
>>>Image.open('b1.png')
<PIL.PngImagePlugin.PngImageFile image mode=P size=640x640 at 0x101856B48>
You want a RGB value. First convert to RGB:
>>>im = Image.open('b1.png')>>>im = im.convert('RGB')>>>im.getpixel((1,1))
(240, 237, 229)
From the docs: http://pillow.readthedocs.org/en/latest/handbook/concepts.html?highlight=mode
P (8-bit pixels, mapped to any other mode using a color palette) ... RGB (3x8-bit pixels, true color)
Post a Comment for "Pil Import Png Pixels As Single Value Instead Of 3 Values Vector"