将多个 RGB 图像读取到 numpy 数组 [英] Reading multiple RGB images to numpy array

查看:72
本文介绍了将多个 RGB 图像读取到 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个RGB图像读取到一个numpy数组.我所有的图像都是分辨率 (32,32,3).我在文件夹中有 10 张图像,我希望我的最终图像"numpy 数组为 (10, 32, 32, 3).我试过下面的代码.

I want to read multiple RGB images to a numpy array. All my images are of resolution (32,32,3). I have 10 images in the folder and I want my final "images" numpy array as (10, 32, 32, 3). I tried below code.

import matplotlib.image as mpimg
import os

folder = 'test_images'
images = np.array([(mpimg.imread(os.path.join(folder, filename))) for filename in os.listdir('test_images')], dtype='uint8')

我遇到错误

    ValueError                                Traceback (most recent call last)
<ipython-input-109-0c5d51212e48> in <module>()
      3 
      4 folder = 'test_images'
----> 5 images = np.array([(mpimg.imread(os.path.join(folder, filename))) for filename in os.listdir('test_images')], dtype='uint8')
      6 
      7 print(len(images))

ValueError: could not broadcast input array from shape (32,32,3) into shape (32,32)

推荐答案

仅概述解决方案,我将执行以下操作:

Just to summarize the solution, I would do the following:

from PIL import Image
import os, numpy as np
folder = 'test_images'

read = lambda imname: np.asarray(Image.open(imname).convert("RGB"))

ims = [read(os.path.join(folder, filename)) for filename in os.listdir(folder)]
im_array = np.array(ims, dtype='uint8')

这篇关于将多个 RGB 图像读取到 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆