Pillow是Python裏的圖像處理庫,具有強大的圖像處理能力,主要包括圖像儲存、圖像顯示、格式轉換以及基本的圖像處理操作等。
如何將pillow的圖像格式和ndarray的格式相互轉換?代碼如下:
from PIL import Image
import numpy as np
# 讀取圖像文件
img = Image.open('test.tif')
print(img.format, img.size, img.mode)
# 將image轉換爲array
pix = np.asarray(img)
# 將array轉換爲image
im = Image.fromarray(np.uint8(pix))
im.show()
相對於matlab,python的庫找起來相對麻煩,但其官網的使用說明非常有用,可作爲首要參考。
Reference:
1. PIL handbook:
2. stackoverflow問答