python-3.x - 我希望能够识别图像的颜色(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()
解决方案
在调色板中获得列表后,将它们分开会更容易。
Image.getpalette() 将图像调色板作为列表返回。
返回: 颜色值列表 [r, g, b, ...],如果图像没有调色板,则返回 None。
https://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.Image.getpalette
推荐阅读
- javascript - JQuery 添加选项以动态选择 7 种不同的选择
- sql-server - 是否会回滚此存储过程中的所有事务
- android - 凌空响应不返回完整数据
- flutter - 尝试获取 pubspec.yaml 时版本解决失败
- python - 使用多线程/多处理加快我的代码的抓取速度
- ios - 检测字符串的语言
- vba - 如何正确地将内置 Office 枚举中的参数从功能区传递到 VBA 宏?
- xamarin - Xamarin Forms 从条目中删除空间
- javascript - 从另一个网站连接到 Sharepoint
- cron - Quartz Cron 表达式问题@每月作业失败