# coding=gbk
from pil import image
import numpy as np
# import scipy
import matplotlib.pyplot as plt
def imagetomatrix(filename):
# 读取图片
im = image.open(filename)
# 显示图片
# im.show()
width,height = im.size
im = im.convert("l")
data = im.getdata()
data = np.matrix(data,dtype=‘float‘)/255.0
#new_data = np.reshape(data,(width,height))
new_data = np.reshape(data,(height,width))
return new_data
# new_im = image.fromarray(new_data)
# # 显示图片
# new_im.show()
def matrixtoimage(data):
data = data*255
new_im = image.fromarray(data.astype(np.uint8))
return new_im
filename = ‘lena.jpg‘
data = imagetomatrix(filename)
print data
new_im = matrixtoimage(data)
plt.imshow(data, cmap=plt.cm.gray, interpolation=‘nearest‘)
new_im.show()
new_im.save(‘lena_1.bmp‘)
上面要先对图片去除颜色,就是变成黑白的,转换成二维数据矩阵,不去颜色的还要保存颜色的,然后后面转换就不行了,下面利用image.fromarray(data) 新建图片
原文:
干货分享
机器学习、深度学习、计算机视觉、自然语言处理及应用案例——干货分享(持续更新……)
来自Lujishu-lujishu.net的文章,链接:http://article.lujishu.net/viewarticle/1281882401170。本文章由作者发布,不代表本站观点,如有侵权,请联系我们撤下该文章。