1

I am trying to find the position in the Array called 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

This code searches each point individually in the Array and if it above the mean its supposed to become 255 if its less 0. How can I find the Position in the Array so I can edit its Value.

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
3
  • 4
    PixelRGB[PixelRGB > ColourMean] = 255? Commented Sep 11, 2017 at 22:54
  • 1
    and PixelRGB[PixelRGB < ColourMean] = 0 Commented Sep 11, 2017 at 22:58
  • Or as a lazy one-liner: PixelRGB = (PixelRGB > ColourMean) * 255 Commented Sep 12, 2017 at 6:01

1 Answer 1

2

As an example, let's consider this array:

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

Now, let's apply your transformation to it:

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

The general form for np.where is np.where(condition, x, y). Wherever condition is true, it returns x. Wherever condition is false, it returns y.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.