from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def ImageToMatrix(filename):
im = Image.open(filename)
width,height = im.size
data = im.getdata()
data = np.matrix(data,dtype='float')/255
new_data = np.reshape(data.getA(),(height,width,3))
return new_data
def MatrixToImage(data):
data = data*255
new_im = Image.fromarray(data.astype(np.uint8))
return new_im
filename = '/Users/gaowenfeng/Documents/图片/头像.png'
data = ImageToMatrix(filename)
new_im = MatrixToImage(data)
plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest')
new_im.show()