在做圖像處理的時候,自己常用的是將PIL的圖片對象轉換成為numpy的數組,同時也將numpy中的數組轉換成為對應的圖片對象。
這里考慮使用PIL來進行圖像的一般處理。
from PIL import Image img = Image.open("lena.jpg") print type(img) img = np.array(img) print type(img) <class 'PIL.JpegImagePlugin.JpegImageFile'> <type 'numpy.ndarray'>
由於PIL也是基於numpy的,因此可以很容易的通過使用numpy中的函數來進行類型的轉換。
img_tr = Image.fromarray(tr_img) print type(img_tr) <class 'PIL.Image.Image'>
可以通過PIL中Image中的fromarray,直接將一個數組對象轉換成為PIL中的圖片對象,然后就可以使用PIL中對應的方法來進行處理了。
