使用python进行图片处理,现在需要读出图片的任意一块区域,并将其转化为一维数组,方便后续卷积操作的使用。
下面使用两种方法进行处理:
convert 函数
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def ImageToMatrix(filename):
im = Image.open(filename) # 读取图片
im.show() # 显示图片
width,height = im.size
print("width is :" + str(width))
print("height is :" + str(height))
im = im.convert("L") # pic --> mat 转换,可以选择不同的模式,下面有函数源码具体说明
data = im.getdata()
data = np.matrix(data,dtype='float')/255.0
new_data = np.reshape(data * 255.0,(height,width))
new_im = Image.fromarray(new_data)
# 显示从矩阵数据得到的图片
new_im.show()
return new_data
def MatrixToImage(data):
data = data*255
new_im = Image.fromarray