I have a list consisting of several sorted grayscale images called imageList. I want to convert each of the images into 2d array and store each of them in a list called arrayList in the same order as it converted. like this:
imageList = ['1.png', '2.png', '3.png', '4.png',....,'1000.png']
arrayList = [[1th_2dArray] , [2th_2dArray], ......, [1000th_2dArray]]
note that I resized my images and converted them from RGB to grayscale and then store them in my imageList.
P.S: I'm new at python and I know for converting an image to an array I can use other methods such as Pillow or OpenCV Library, any suggestion would be appreciated.
CodePudding user response:
IIUC, you have a list of paths to images that you want to convert to arrays, so you can try something like this:
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)
So each image is loaded and then converted to an array. Afterwards, the channel dimension is omitted, resulting in 2D arrays.