如何使用keras将列表中的每个灰度图像转换为二维数组?

发布于 2025-01-13 01:43:07 字数 399 浏览 1 评论 0原文

我有一个由多个排序的灰度图像组成的列表,称为 imageList。我想将每个图像转换为二维数组,并将它们按照转换的顺序存储在名为 arrayList 的列表中。像这样:

imageList = ['1.png', '2.png', '3.png', '4.png',....,'1000.png']

arrayList = [[1th_2dArray] , [2th_2dArray], ......, [1000th_2dArray]]

请注意,我调整了图像大小并将它们从 RGB 转换为灰度,然后将它们存储在我的图像列表。

PS:我是 python 新手,我知道为了将图像转换为数组,我可以使用其他方法,例如 Pillow 或 OpenCV 库,任何建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写下不归期 2025-01-20 01:43:07

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)

因此每个图像都会被加载,然后转换为数组。然后,省略通道维度,得到二维数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文