# coding=gbk from PIL import Image import numpy as np # import scipy def loadImage(): # 讀取圖片 im = Image.open("lena.jpg") # 顯示圖片 im.show() im = im.convert("L") data = im.getdata() data = np.matrix(data) # print data # 變換成512*512 data = np.reshape(data,(512,512)) new_im = Image.fromarray(data) # 顯示圖片 new_im.show() loadImage()
上面要先對圖片去除顏色,就是變成黑白的,轉換成二維數據矩陣,不去顏色的還要保存顏色的,然后后面轉換就不行了,下面利用Image.fromarray(data) 新建圖片
轉換后