In [1]:
from scipy import misc
import matplotlib.pyplot as plt

In [2]:
plt.imshow(misc.face())


Out[2]:
<matplotlib.image.AxesImage at 0x883a9b0>

In [3]:
plt.show()



In [4]:
plt.imsave('face.png', misc.face())

In [5]:
img = misc.imread('face.png')

In [6]:
import numpy

In [7]:
type(img)


Out[7]:
numpy.ndarray

In [8]:
img.shape


Out[8]:
(768, 1024, 4)

In [9]:
img


Out[9]:
array([[[121, 112, 131, 255],
        [138, 129, 148, 255],
        [153, 144, 165, 255],
        ..., 
        [119, 126,  74, 255],
        [131, 136,  82, 255],
        [139, 144,  90, 255]],

       [[ 89,  82, 100, 255],
        [110, 103, 121, 255],
        [130, 122, 143, 255],
        ..., 
        [118, 125,  71, 255],
        [134, 141,  87, 255],
        [146, 153,  99, 255]],

       [[ 73,  66,  84, 255],
        [ 94,  87, 105, 255],
        [115, 108, 126, 255],
        ..., 
        [117, 126,  71, 255],
        [133, 142,  87, 255],
        [144, 153,  98, 255]],

       ..., 
       [[ 87, 106,  76, 255],
        [ 94, 110,  81, 255],
        [107, 124,  92, 255],
        ..., 
        [120, 158,  97, 255],
        [119, 157,  96, 255],
        [119, 158,  95, 255]],

       [[ 85, 101,  72, 255],
        [ 95, 111,  82, 255],
        [112, 127,  96, 255],
        ..., 
        [121, 157,  96, 255],
        [120, 156,  94, 255],
        [120, 156,  94, 255]],

       [[ 85, 101,  74, 255],
        [ 97, 113,  84, 255],
        [111, 126,  97, 255],
        ..., 
        [120, 156,  95, 255],
        [119, 155,  93, 255],
        [118, 154,  92, 255]]], dtype=uint8)

In [10]:
img[img < 128] = 0
img[img >= 128] = 255

In [11]:
plt.imshow(img)


Out[11]:
<matplotlib.image.AxesImage at 0x8daa358>

In [12]:
plt.show()



In [13]:
img


Out[13]:
array([[[  0,   0, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ..., 
        [  0,   0,   0, 255],
        [255, 255,   0, 255],
        [255, 255,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [255,   0, 255, 255],
        ..., 
        [  0,   0,   0, 255],
        [255, 255,   0, 255],
        [255, 255,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ..., 
        [  0,   0,   0, 255],
        [255, 255,   0, 255],
        [255, 255,   0, 255]],

       ..., 
       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ..., 
        [  0, 255,   0, 255],
        [  0, 255,   0, 255],
        [  0, 255,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ..., 
        [  0, 255,   0, 255],
        [  0, 255,   0, 255],
        [  0, 255,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ..., 
        [  0, 255,   0, 255],
        [  0, 255,   0, 255],
        [  0, 255,   0, 255]]], dtype=uint8)

In [14]:
imgL = misc.imread('face.png', mode='L')

In [15]:
imgL.shape


Out[15]:
(768, 1024)

In [16]:
imgL[0]


Out[16]:
array([116, 133, 149, ..., 117, 128, 136], dtype=uint8)

In [19]:
misc.imsave('faceL.png', imgL)

In [20]:
imgL[imgL < 128] = 0
imgL[imgL >= 128] = 255

misc.imsave('faceL_bw.png', imgL)

In [ ]: