PIL将png像素作为单个值而不是3个值矢量导入 [英] PIL import png pixels as single value instead of 3 values vector
问题描述
我有一堆我从Google地图下载的png格式的地图文件,我想将其转换为一个较大的图像.当我导入一个像素并查看像素时,我看到像素是0..256范围内的单个数字,而不是三个值列表.这是怎么回事?
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 the pixels are a single number in the range of 0..256 instead of three values list. What's going on here?
我正在使用
from PIL import Image
print open('b1.png').load()[0,0]
我得到的是153
而不是[r,g,b]
and I get 153
instead of [r,g,b]
我的图像文件是
推荐答案
出现此结果的原因([0,0]
中的值153)是图像模式设置为P
(8位像素,映射到其他任何像素)模式使用调色板).如果要设置其他模式(例如RGB
),则可以在调用方法load()
之前进行设置.
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]
打印结果为
(240, 237, 229)
有关图像模式的更多信息,请访问文档.
For more information about Image Modes please visit the documentation.
这篇关于PIL将png像素作为单个值而不是3个值矢量导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!