使用PIL/Pillow识别带有调色板的图像的颜色值 [英] Identify the color values of an image with a color palette using PIL/Pillow

查看:673
本文介绍了使用PIL/Pillow识别带有调色板的图像的颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PIL/枕头识别图像使用的调色板的颜色. 我尝试了以下方法:

I'm trying to identify the colors of the used color palette of an image with PIL/pillow. I've tried the following:

  • 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 ...])
  • image[x,y]: this will only give me the index number of the corresponding pixel (i.e. 1)
  • image.getpixel((x,y)): again, this will only give me the index number of the corresponding pixel (i.e. 1)
  • image.getcolors(): This will give me the number of pixels and their corresponding index number (i.e. [(2, 1), (2, 0)])
  • image.palette: Returns a "PIL.ImagePalette.ImagePalette object"
  • image.getpalette(): Returns a large array of (to me seemingly) unrelated integers (i.e. [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.

使用此示例图像(2x2像素图像,创建2种颜色的索引模式使用GIMP,顶部的两个像素是红色(255,0,0)底部的两个像素是蓝色(0,0,255)),我期望的是:

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:

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


我最近的是:


The closest I have is:

image.palette.getdata():这给了我('RGB;L', b'\x00\x00\xff\xff\x00\x00').有什么办法可以将此映射到索引号.我认为,这里每三个字节将映射到一个索引号.

image.palette.getdata(): This gives me ('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))

然后仅打印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)    

colours设置为:

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

counts为:

array([2, 2])

这篇关于使用PIL/Pillow识别带有调色板的图像的颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆