原文链接
应用Python读取二维数组,将二维数组输入为图片,并保留在本地。
代码如下:
# coding=utf8from PIL import Imageimport numpy as npfrom scipy import miscimport matplotlib.pyplot as pyplot a = 300b = 500x = 20y = 20w = 40h = 80 def Gener_mat(a,b,x,y,w,h): #生成图片矩阵 img_mat = np.zeros((a, b), dtype=np.int) for i in range(0,a): for j in range(0,b): img_mat[i][j] = 0 for i in range(x,x+w): for j in range(y,y+h): img_mat[i][j] = 1 return img_matdef out_img(data): #输入图片 new_im = Image.fromarray(data) #调用Image库,数组归一化 #new_im.show() pyplot.imshow(data) #显示新图片 misc.imsave('new_img.jpg', new_im) #保留图片到本地img_mat = Gener_mat(a,b,x,y,w,h)out_img(img_mat)
学习更多编程常识,请关注我的公众号:
代码的路