Search code examples
pythonnumpymnist

Numpy grayscale image to black and white


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?


Solution

  • 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]])