1. 程式人生 > >Python圖片轉換成矩陣,矩陣資料轉換成圖片

Python圖片轉換成矩陣,矩陣資料轉換成圖片

# 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()