In addition to unutbus answer, you might also want to take care when converting to grayscale: uniform weighting (taking the mean) is not the only, or necessarily correct, way to go.
From the wikipedia page
Y = 0.2126 R + 0.7152 G + 0.0722 B
which translates into
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img = mpimg.imread('image.png')
arr = 0.2126* img[...,0] + 0.7152* img[...,1] + 0.0722 * img[...,2]
plt.imshow(arr, cmap=plt.get_cmap('gray'))
plt.show()
Note in response to question
The array is now stored in arr
which you can now manipulate as you please.
Silhouette
If you need a silhouette / mask, you can use simple thresholding
bw = arr > 0.3
plt.imshow(bw)
plt.show()
No need to add in the colormap argument, though the default is jet
, which will display as blue and red for a binary image.
Output
To save the image you can use plt.savefig
which will use the colormap applied to the image. This means if you want a grayscale image you need to specify the 'gray' colormap
Alternatively using e.g. scipy.misc.imsave
will save a 2d matrix as grayscale. Other libraries you could use include the png module