首页 > 解决方案 > 我希望能够识别图像的颜色(Python3)

问题描述

该图像分为3种颜色,但我希望能够在Python3中识别图像的颜色。请教。

from PIL import Image
import PIL
import numpy as np


png_path = 'test.png'
img_array = np.asarray(Image.open(png_path))


Image.fromarray(img_array).show()
img_array = np.asarray(Image.open(png_path))[:,:,3]
print(np.unique(img_array))

##### I want to know the condition judgment method######
img_array_2 = np.where((img_array >=1) & (img_array <= 150), 0 , img_array)
##################

# check
imgPIL = Image.fromarray(img_array_2)
imgPIL.show()

在此处输入图像描述

标签: python-3.xpython-imaging-library

解决方案


在调色板中获得列表后,将它们分开会更容易。

Image.getpalette() 将图像调色板作为列表返回。

返回: 颜色值列表 [r, g, b, ...],如果图像没有调色板,则返回 None。

https://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.Image.getpalette


推荐阅读