【发布时间】:2020-03-11 17:52:42
【问题描述】:
我想遍历 PNG 图像(我使用 imagemagick 创建的)中给定颜色的所有像素。我可以处理的迭代部分,但该图像使用调色板,因此我需要获取给定颜色的调色板索引,在我的情况下为白色。通常这应该与函数ImagePalette.getcolor() 一起使用,但这告诉我调色板处于原始模式,我不知道如何禁用。我所做的如下,但必须有一个更简单的方法。
import sys
from PIL import Image
import numpy as np
image = Image.open( sys.argv[1] )
width, height = image.size
# image.palette.getcolor( (255,255,255) ) # gives ValueError: palette contains raw palette data
palette = image.getpalette() # a list
palette = np.reshape( palette, (-1, 3) )
#rows255, _cols255 = np.where( palette == (255,255,255) ) # gives the same as below - apparently or'ing
rows255, _cols255 = np.where( palette == 255 )
whites = []
count = 1
prev = -1
for r in rows255:
if r==prev:
count += 1
else:
count = 1
if count==3:
whites.append( prev )
prev = r
print(whites)
所以我有兴趣直接使用 pil 或使用 numpy.where() 正确处理元组。
每个带有调色板的图像的情况都应该相同,但这是我的文件之一:https://imgur.com/ojul9mf
【问题讨论】:
-
请张贴“不开心”图片。
-
您是否要计算白色像素?如果是这样,您最好先进行阈值处理,然后计算非零像素。
-
我正在尝试计算所有满足我无法在图像上检查的标准的像素,因此我需要白色像素的所有 (x,y) 坐标,以便我可以进一步评估。跨度>
-
另外,你能解释一下你正在寻找的标准吗?可能有更好的方法来找到您要寻找的任何东西。
标签: imagemagick pil python numpy python-imaging-library