【发布时间】:2020-03-05 16:20:31
【问题描述】:
我正在尝试使用 PIL/pillow 识别图像中使用的调色板的颜色。 我尝试了以下方法:
-
image[x,y]:这只会给我相应像素的索引号(即1) -
image.getpixel((x,y)):同样,这只会给我相应像素的索引号(即1) -
image.getcolors():这会给我像素数和它们对应的索引号(即[(2, 1), (2, 0)]) -
image.palette:返回一个“PIL.ImagePalette.ImagePalette 对象” -
image.getpalette():返回一大堆(在我看来)不相关的整数(即[0, 0, 255, 255, 0, 0, 2, 2, 2, 3, 3 ,3 ...])
作为绝对的后备,我可以转换图像模式然后获取颜色值,但如果可能的话我宁愿不这样做。
有了这个example image(2x2 像素图像,使用 GIMP 创建的具有 2 种颜色的索引模式,顶部两个像素是红色 (255,0,0),底部两个是蓝色 (0,0,255)),我期待类似:
image.getpalette()
1: (255,0,0)
0: (0,0,255)
编辑:我最接近的是:
image.palette.getdata():这给了我('RGB;L', b'\x00\x00\xff\xff\x00\x00')。有没有办法把它映射到索引号。我想,这里每三个字节都会映射到一个索引号。
【问题讨论】:
-
合并
getcolors和getpalette。那些“看似无关的整数”正是您要寻找的;它们很重要。 -
好的,谢谢您的评论。我应该如何组合它们?
-
imgage.palette.colors包含从palette colors到palette indices的映射,例如{(255, 0, 0): 0, (0, 0, 255): 1, (0, 255, 0): 2, (255, 255, 255): 3, (0, 255, 255): 4, (255, 255, 0): 5}
标签: python-3.x image python-imaging-library pixel