Python 在numpy数组中查找值的位置
我试图在名为ImageArray的数组中找到位置Python 在numpy数组中查找值的位置,python,arrays,numpy,python-imaging-library,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[:,:
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
对于ArrayRGB中的行:
对于行中的像素RGB:
#分别查看每个像素
打印(像素RGB)
如果(像素RGB>颜色平均值):
PixelRGB[PositionPixel]=255
elif(像素RGB<颜色平均值):
PixelRGB[PositionPixel]=0
<代码> > p>作为一个例子,让我们考虑一下这个数组:
>>> 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(条件,x,y)
。只要条件
为真,它就会返回x
。只要条件
为false,它就会返回y
PixelRGB[PixelRGB>colorMean]=255
?和PixelRGB[PixelRGB
或作为惰性单行程序返回:PixelRGB=(PixelRGB>colorMean)*255