1. 程式人生 > >將Numpy陣列儲存為影象的幾種方法

將Numpy陣列儲存為影象的幾種方法

將Numpy陣列儲存為影象,有以下幾種方法:

1、使用scipy.misc

程式碼如下:

from PIL import Image
import numpy as np
from scipy import misc

# 首先在該py檔案所在目錄下隨便放一張圖片,使用PIL.Image庫的open方法開啟
image = Image.open("0_train.jpg")
# 使用numpy將該圖片的二進位制資料轉換成多維陣列形式
image_array = np.array(image)
# 列印該陣列
print(image_array)
# 使用misc.imsave方法將陣列儲存為圖片
misc.imsave('out.jpg', image_array)

執行結果如下:

[[[244 244 242]
  [244 244 242]
  [246 245 243]
  ...
  [255 252 249]
  [250 249 245]
  [239 240 235]]

 [[251 250 248]
  [249 248 244]
  [247 246 244]
  ...
  [236 232 229]
  [253 252 248]
  [251 252 247]]

 [[252 248 245]
  [249 246 241]
  [245 241 238]
  ...
  [247 243 240]
  [245 244 240]
  [250 251 246]]

 ...

 [[249 244 241]
  [250 245 242]
  [251 246 243]
  ...
  [251 237 236]
  [242 228 227]
  [255 251 250]]

 [[248 242 242]
  [250 244 244]
  [251 245 245]
  ...
  [249 233 233]
  [250 234 234]
  [255 243 243]]

 [[249 243 243]
  [250 244 244]
  [251 245 245]
  ...
  [255 242 242]
  [255 250 250]
  [249 233 233]]]

Process finished with exit code 0

py檔案所在的目錄下生成了out.jpg圖片,且該圖片和0_train.jpg圖片相同。


scipy.imsave方法會標準化所有影象,使陣列中的最小值min(資料)變成黑色,最大值max(資料)變成白色。

如果陣列資料是精確的灰度級或準確的RGB通道的資料,則程式碼如下:

from PIL import Image
import numpy as np
# 使用PIL庫和numpy庫只是為了快速得到一個可以用於儲存為圖片的陣列,即從現有的圖片直接轉換成陣列
from scipy import misc

# 首先在該py檔案所在目錄下隨便放一張圖片,使用PIL.Image庫的open方法開啟
image = Image.open("0_train.jpg")
# 使用numpy將該圖片的二進位制資料轉換成多維陣列形式
image_array = np.array(image)
# 列印該陣列
print(image_array)
# 使用misc.imsave方法將陣列儲存為圖片
misc.toimage(image_array, cmin=0.0, cmax=...).save('outfile.jpg')

執行結果如下:

[[[244 244 242]
  [244 244 242]
  [246 245 243]
  ...
  [255 252 249]
  [250 249 245]
  [239 240 235]]

 [[251 250 248]
  [249 248 244]
  [247 246 244]
  ...
  [236 232 229]
  [253 252 248]
  [251 252 247]]

 [[252 248 245]
  [249 246 241]
  [245 241 238]
  ...
  [247 243 240]
  [245 244 240]
  [250 251 246]]

 ...

 [[249 244 241]
  [250 245 242]
  [251 246 243]
  ...
  [251 237 236]
  [242 228 227]
  [255 251 250]]

 [[248 242 242]
  [250 244 244]
  [251 245 245]
  ...
  [249 233 233]
  [250 234 234]
  [255 243 243]]

 [[249 243 243]
  [250 244 244]
  [251 245 245]
  ...
  [255 242 242]
  [255 250 250]
  [249 233 233]]]

Process finished with exit code 0

py檔案所在目錄下生成outfile.jpg圖片,且該圖片和0_train圖片一樣。

2、使用PIL庫

程式碼如下:

from PIL import Image
import numpy as np

# 開啟圖片
im = Image.open("0_train.jpg")
# 將圖片轉化為numpy陣列
im_array = np.array(im)
# 列印陣列
print(im_array)
# 將陣列轉化回圖片
img = Image.fromarray(im_array).convert('RGB')
# 將陣列儲存為圖片
img.save("out.bmp")

執行結果如下:

[[[244 244 242]
  [244 244 242]
  [246 245 243]
  ...
  [255 252 249]
  [250 249 245]
  [239 240 235]]

 [[251 250 248]
  [249 248 244]
  [247 246 244]
  ...
  [236 232 229]
  [253 252 248]
  [251 252 247]]

 [[252 248 245]
  [249 246 241]
  [245 241 238]
  ...
  [247 243 240]
  [245 244 240]
  [250 251 246]]

 ...

 [[249 244 241]
  [250 245 242]
  [251 246 243]
  ...
  [251 237 236]
  [242 228 227]
  [255 251 250]]

 [[248 242 242]
  [250 244 244]
  [251 245 245]
  ...
  [249 233 233]
  [250 234 234]
  [255 243 243]]

 [[249 243 243]
  [250 244 244]
  [251 245 245]
  ...
  [255 242 242]
  [255 250 250]
  [249 233 233]]]

Process finished with exit code 0

生成的圖片如下:

使用PIL庫儲存圖片的最大好處是,你可以將圖片儲存為目前支援的幾乎所有主流圖片格式,如jpg,png,bmp等。

3、使用matplotlib的兩種方法

程式碼如下:

from PIL import Image
import numpy as np
# 使用PIL庫和numpy是隻是為了快速得到一個可以用於儲存為圖片的陣列,即從現有的圖片直接轉換成陣列
from matplotlib import image

# 開啟圖片
im = Image.open("0_train.jpg")
# 將圖片轉化為numpy陣列
im_array = np.array(im)
# 列印陣列
print(im_array)
# 儲存圖片
image.imsave("out_plt.png", im_array)

執行結果如下:

[[[244 244 242]
  [244 244 242]
  [246 245 243]
  ...
  [255 252 249]
  [250 249 245]
  [239 240 235]]

 [[251 250 248]
  [249 248 244]
  [247 246 244]
  ...
  [236 232 229]
  [253 252 248]
  [251 252 247]]

 [[252 248 245]
  [249 246 241]
  [245 241 238]
  ...
  [247 243 240]
  [245 244 240]
  [250 251 246]]

 ...

 [[249 244 241]
  [250 245 242]
  [251 246 243]
  ...
  [251 237 236]
  [242 228 227]
  [255 251 250]]

 [[248 242 242]
  [250 244 244]
  [251 245 245]
  ...
  [249 233 233]
  [250 234 234]
  [255 243 243]]

 [[249 243 243]
  [250 244 244]
  [251 245 245]
  ...
  [255 242 242]
  [255 250 250]
  [249 233 233]]]

Process finished with exit code 0

生成的圖片如圖所示。

使用matplotlib還可以用pyplot的繪圖功能來用陣列的資料繪製出圖片再儲存。

程式碼如下:

from PIL import Image
import numpy as np
# 使用PIL庫和numpy是隻是為了快速得到一個可以用於儲存為圖片的陣列,即從現有的圖片直接轉換成陣列
import matplotlib.pyplot as plt

# 開啟圖片
im = Image.open("0_train.jpg")
# 將圖片轉化為numpy陣列
im_array = np.array(im)
# 列印陣列
print(im_array)
# 繪製圖片
plt.imshow(im_array)
# 儲存圖片
plt.savefig("out_plt2.png")

執行結果如下:

[[[244 244 242]
  [244 244 242]
  [246 245 243]
  ...
  [255 252 249]
  [250 249 245]
  [239 240 235]]

 [[251 250 248]
  [249 248 244]
  [247 246 244]
  ...
  [236 232 229]
  [253 252 248]
  [251 252 247]]

 [[252 248 245]
  [249 246 241]
  [245 241 238]
  ...
  [247 243 240]
  [245 244 240]
  [250 251 246]]

 ...

 [[249 244 241]
  [250 245 242]
  [251 246 243]
  ...
  [251 237 236]
  [242 228 227]
  [255 251 250]]

 [[248 242 242]
  [250 244 244]
  [251 245 245]
  ...
  [249 233 233]
  [250 234 234]
  [255 243 243]]

 [[249 243 243]
  [250 244 244]
  [251 245 245]
  ...
  [255 242 242]
  [255 250 250]
  [249 233 233]]]

Process finished with exit code 0

生成圖片如下。注意這種方式生成的圖片預設是帶座標軸的。你可以使用pyplot中相關方法隱藏座標軸。另外這種繪製出的圖片四周有空白。

原圖:

生成圖:

4、使用opencv庫

程式碼如下:

from PIL import Image
import numpy as np
import cv2

# 開啟圖片
im = Image.open("0_train.jpg")
# 將圖片轉化為numpy陣列
im_array = np.array(im)
# 列印陣列
print(im_array)
# 儲存圖片
cv2.imwrite("out_cv2.jpg", im_array)

執行結果如下:

[[[244 244 242]
  [244 244 242]
  [246 245 243]
  ...
  [255 252 249]
  [250 249 245]
  [239 240 235]]

 [[251 250 248]
  [249 248 244]
  [247 246 244]
  ...
  [236 232 229]
  [253 252 248]
  [251 252 247]]

 [[252 248 245]
  [249 246 241]
  [245 241 238]
  ...
  [247 243 240]
  [245 244 240]
  [250 251 246]]

 ...

 [[249 244 241]
  [250 245 242]
  [251 246 243]
  ...
  [251 237 236]
  [242 228 227]
  [255 251 250]]

 [[248 242 242]
  [250 244 244]
  [251 245 245]
  ...
  [249 233 233]
  [250 234 234]
  [255 243 243]]

 [[249 243 243]
  [250 244 244]
  [251 245 245]
  ...
  [255 242 242]
  [255 250 250]
  [249 233 233]]]

Process finished with exit code 0

生成圖片如下: