在numpy数组中找到值的位置

时间:2017-09-11 22:51:47

标签: python arrays numpy python-imaging-library

我试图在数组中找到名为ImageArray的位置

from PIL import Image, ImageFilter
import numpy as np

ImageLocation = Image.open("images/numbers/0.1.png")

#Creates an Array [3d] of the image for the colours
ImageArray = np.asarray(ImageLocation)
ArrayRGB = ImageArray[:,:,:3]
#Removes the Alpha Value from the output

print(ArrayRGB)
#RGB is output

print(round(np.mean(ArrayRGB),2))
ColourMean = np.mean(ArrayRGB)
#Outputs the mean of all the values for RGB in each pixel

此代码在数组中单独搜索每个点,如果它高于平均值,如果它小于0则应该变为255.如何在数组中找到位置以便我可以编辑它的值。

for Row in ArrayRGB:
    for PixelRGB in Row:
        #Looks at each pixel individually
        print(PixelRGB)
        if(PixelRGB > ColourMean):
            PixelRGB[PositionPixel] = 255
        elif(PixelRGB < ColourMean):
            PixelRGB[PositionPixel] = 0

1 个答案:

答案 0 :(得分:2)

举个例子,让我们考虑一下这个数组:

>>> import numpy as np
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

现在,让我们将转换应用到它:

>>> np.where(a>a.mean(), 255, 0)
array([[  0,   0,   0],
       [  0,   0, 255],
       [255, 255, 255]])

np.where的一般表单是np.where(condition, x, y)。只要condition为真,它就会返回x。只要condition为false,就会返回y