1. 程式人生 > 其它 >python - 圖片和numpy陣列之間的相互轉化 及 各自的視覺化

python - 圖片和numpy陣列之間的相互轉化 及 各自的視覺化

import PIL.Image as image
import numpy as np
a = image.open("./weixin.jpg")
# 獲取影象
print("type (a): ", type(a))  #  <class 'PIL.JpegImagePlugin.JpegImageFile'> #
a.show()

# 從影象轉換為陣列
b = np.array(a)
print("type(b): ", type(b))   #  <class 'numpy.ndarray'>
# 將陣列形成的影象視覺化
plt.imshow(b) plt.show() # 從陣列轉換為影象 c = image.fromarray(b) # 從陣列到影象 print("type(c): ", type(c)) # <class 'PIL.Image.Image'> # 直接將影象視覺化 c.show()