简体   繁体   English

使用 PIL/Pillow 使用调色板识别图像的颜色值

[英]Identify the color values of an image with a color palette using PIL/Pillow

I'm trying to identify the colors of the used color palette of an image with PIL/pillow.我正在尝试使用 PIL/枕头识别图像的已用调色板的颜色。 I've tried the following:我尝试了以下方法:

  • image[x,y] : this will only give me the index number of the corresponding pixel (ie 1 ) image[x,y] :这只会给我相应像素的索引号(即1
  • image.getpixel((x,y)) : again, this will only give me the index number of the corresponding pixel (ie 1 ) image.getpixel((x,y)) :同样,这只会给我相应像素的索引号(即1
  • image.getcolors() : This will give me the number of pixels and their corresponding index number (ie [(2, 1), (2, 0)] ) image.getcolors() :这会给我像素数和它们对应的索引号(即[(2, 1), (2, 0)]
  • image.palette : Returns a "PIL.ImagePalette.ImagePalette object" image.palette :返回“PIL.ImagePalette.ImagePalette 对象”
  • image.getpalette() : Returns a large array of (to me seemingly) unrelated integers (ie [0, 0, 255, 255, 0, 0, 2, 2, 2, 3, 3 ,3 ...] ) image.getpalette() :返回一个大数组(对我来说似乎是)不相关的整数(即[0, 0, 255, 255, 0, 0, 2, 2, 2, 3, 3 ,3 ...]

As an absolute fallback, I could convert the image mode and then get the color values, but I'd rather not if possible.作为绝对后备,我可以转换图像模式,然后获取颜色值,但如果可能的话,我宁愿不这样做。

With this example image (2x2 pixel image, indexed mode with 2 colors created with GIMP, the top two pixels are red (255,0,0) the bottom two are blue (0,0,255)), I was expecting something like:使用此示例图像(2x2 像素图像,使用 GIMP 创建的 2 种颜色的索引模式,顶部两个像素是红色 (255,0,0),底部两个像素是蓝色 (0,0,255)),我期待以下内容:

image.getpalette()
1: (255,0,0)
0: (0,0,255)

Edit: The closest I have is:编辑:我最接近的是:

image.palette.getdata() : This gives me ('RGB;L', b'\\x00\\x00\\xff\\xff\\x00\\x00') . image.palette.getdata() :这给了我('RGB;L', b'\\x00\\x00\\xff\\xff\\x00\\x00') Is there any way to have this mapped to the index number.有什么办法可以将它映射到索引号。 Here each three bytes would map to one index number, I'd reckon.我认为,这里每三个字节将映射到一个索引号。

You can get and arrange the palette like this:您可以像这样获取和排列调色板:

import numpy as np
from PIL import Image

# Open image
im = Image.open('a.png')

# Get palette and reshape into 3 columns
palette = np.array(im.getpalette(),dtype=np.uint8).reshape((256,3))

Then just print palette .然后只需打印palette

[[  0   0 255]      <--- first entry is blue
 [255   0   0]      <--- second is red
 [  2   2   2]      <--- grey padding to end
 [  3   3   3]
 [  4   4   4]
 [  5   5   5]
 [  6   6   6]
 ...
 ...
 [253 253 253]
 [254 254 254]
 [255 255 255]]

If you want to count the colours and how many of each there are, do this:如果要计算颜色以及每种颜色的数量,请执行以下操作:

# Convert Image to RGB and make into Numpy array
na = np.array(im.convert('RGB')) 

# Get used colours and counts of each
colours, counts = np.unique(na.reshape(-1,3), axis=0, return_counts=1)    

That gives colours as:这给出了colours

array([[  0,   0, 255],
       [255,   0,   0]], dtype=uint8)

and counts as:counts

array([2, 2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM