我有一個串列,其中包含幾個名為 imageList 的已排序灰度影像。我想將每個影像轉換為二維陣列,并將它們中的每一個存盤在一個名為 arrayList 的串列中,其順序與轉換后的順序相同。像這樣:
imageList = ['1.png', '2.png', '3.png', '4.png',....,'1000.png']
arrayList = [[1th_2dArray] , [2th_2dArray], ......, [1000th_2dArray]]
請注意,我調整了影像的大小并將它們從 RGB 轉換為灰度,然后將它們存盤在我的 imageList 中。
PS:我是 python 的新手,我知道將影像轉換為陣列我可以使用其他方法,例如 Pillow 或 OpenCV Library,任何建議都將不勝感激。
uj5u.com熱心網友回復:
IIUC,您有一個要轉換為陣列的影像的路徑串列,因此您可以嘗試以下操作:
import tensorflow as tf
import numpy as np
image_list = ['/content/1.png', '/content/2.png', '/content/3.png']
array_list = [np.squeeze(tf.keras.preprocessing.image.img_to_array(tf.keras.preprocessing.image.load_img(path, color_mode='grayscale')), axis=-1) for path in image_list]
print(array_list[0].shape)
(100, 100)
所以每個影像都被加載,然后轉換為一個陣列。之后,通道維度被省略,產生二維陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/441556.html
標籤:Python 麻木的 opencv 喀拉斯 python成像库
上一篇:連接矩陣5x2和5x3
