1

I use the MNIST dataset that contains 28x28 grayscale images represented as numpy arrays with 0-255 values. I'd like to convert images to black and white only (0 and 1) so that pixels with a value over 128 will get the value 1 and pixels with a value under 128 will get the value 0.

Is there a simple method to do so?

1 Answer 1

4

Yes. Use (arr > 128) to get a boolean mask array of the same shape as your image, then .astype(int) to cast the bools to ints:

>>> import numpy as np
>>> arr = np.random.randint(0, 255, (5, 5))
>>> arr
array([[153, 167, 141,  79,  58],
       [184, 107, 152, 215,  69],
       [221,  90, 172, 147, 125],
       [ 93,  35, 125, 186, 187],
       [ 19,  72,  28,  94, 132]])
>>> (arr > 128).astype(int)
array([[1, 1, 1, 0, 0],
       [1, 0, 1, 1, 0],
       [1, 0, 1, 1, 0],
       [0, 0, 0, 1, 1],
       [0, 0, 0, 0, 1]])
Sign up to request clarification or add additional context in comments.

3 Comments

or arr => 128 (128 black and 128 white values)
Sure. OP asked for "value over 128", so that's what this does :)
have to correct my first remark: arr >= 128 (not =>)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.