[英]Python PIL(Pillow): Convert a list of images into np.array efficiently?
我想将图像列表有效地转换为np.array。
我必须处理一些用PIL.Image.open(img_path)
读取的jpg图像,然后我必须处理每个图像,然后将它们全部放入一个列表中,然后将其完全转换为np.array
我想要的形状是(N,H,W,C)
,分别表示number of img
, height
, width
, channel
。
我的尝试:
all_img = []
for filename in all_filename_of_img[:100]:
// process each img
// then append each into the list
all_img.append(img)
all_np_img = np.arrray(all_img, dtype=np.array)
错误:
Traceback (most recent call last):
File ".../playground.py", line 39, in <module>
all_np_img = np.array(all_img, dtype=np.array)
TypeError: data type not understood
如果我先通过img = np.asarray(img)
将for循环中的每个img转换,然后执行all_img.append(img)
,这是正确的方法吗?
在这种情况下,可以使用一个名为stack
的numpy函数:
all_np_img = np.stack(all_img)
使用np.vstack :
pics = list()
for filename in all_filename_of_img[:100]:
img = Image.open(filename)
pics.append(img)
np.vstack(pics)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.